2015-03-02 474 views
1

我使用htaccess在我的項目中生成乾淨的URL。.htaccess重寫規則不起作用?

一樣,我想這個URL轉換:

https://localhost/erp/profile.php?username=kopyo 

到這個網址:

https://localhost/erp/profile/kopyo 

我htaccess的代碼是:

Options +FollowSymLinks -MultiViews 
# Turn mod_rewrite on 
RewriteEngine On 
RewriteBase /erp 

RewriteCond %{HTTPS} !on 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} 

# Code I am using is this line but not working 
RewriteRule profile/(.*) profile.php?username=$1 

# To externally redirect /dir/foo.php to /dir/foo 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC] 
RewriteRule^%1 [R,L] 

# To internally forward /dir/foo to /dir/foo.php 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule ^(.*?)/?$ $1.php [L] 

讓我知道我在做什麼在這個錯誤。由於

+0

它是否給'HTTP 1.1 500內部服務器Error'?或重定向錯誤?或... – 2015-03-02 12:02:57

+0

請參考下面的鏈接,你會得到你的答案: http://stackoverflow.com/questions/18929925/url-rewrite-htaccess – 2015-03-02 12:03:31

回答

0

確保使用正確的標誌RewriteRule後重新安排你的規則:

Options +FollowSymLinks -MultiViews 
# Turn mod_rewrite on 
RewriteEngine On 
RewriteBase /erp/ 

RewriteCond %{HTTPS} off 
RewriteRule^https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]  

# To externally redirect /dir/foo.php to /dir/foo 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC] 
RewriteRule^%1 [R=301,NE,L] 

RewriteRule ^profile/(.+)$ profile.php?username=$1 [L,NC,QSA] 

# To internally forward /dir/foo to /dir/foo.php 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule ^(.+?)/?$ $1.php [L] 
+0

它仍然沒有工作,當我添加尾隨哈希( /)到網址,一切都搞砸了。 Css,圖像根本不會加載。 – vitorio 2015-03-02 12:47:41

+0

css/js是一個不同的問題,只是意味着規則工作正常。對於css/js/image等,您可以在頁面的HTML:''的''部分添加此項,以便每個相對URL都從該URL解析,而不是當前頁面的URL。 – anubhava 2015-03-02 12:49:52

+0

不,我仍然無法訪問這個'https:// localhost/erp/profile/kopyo'。仍然沒有發現錯誤。 – vitorio 2015-03-02 12:53:39