2016-10-03 44 views
1

我目前的.htaccess看起來是這樣的:破碎的.htaccess重寫

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^example.com$ [NC] 
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301] 
RewriteRule /([^/.]+)/?$ /restaurants.php?id=$1 [L,NC,QSA] 
RewriteCond %{THE_REQUEST} ^.*/index\.php 
RewriteRule ^(.*)index.php$ /$1 [R=301,L] 

我想我的網址是搜索引擎友好,我想出如何重寫example.com/restaurants.php?id=123到www.example.com/name/123。

問題是,當你去非www的example.com/name/123,它重定向到醜陋的網址example.com/restaurants.php?id=123,然後重定向到SEO友好乾淨www .example.com/name/123

如何解決非www版本的問題?

謝謝你的時間!

回答

0

有你的規則順序:

RewriteEngine On 

RewriteCond %{HTTP_HOST} ^example\.com$ [NC] 
RewriteRule^http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE] 

RewriteCond %{THE_REQUEST} ^.*/index\.php 
RewriteRule ^(.*)index.php$ /$1 [R=301,L,NE] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule /([^/.]+)/?$ /restaurants.php?id=$1 [L,NC,QSA] 

確保清除瀏覽器緩存whiel測試此。

+0

這不起作用。您的示例允許www和非www來解析200 –

+0

不,首先規則會將所有非www URL重定向到www URL。您必須清除瀏覽器緩存,並確保www和非www都指向此htaccess – anubhava

+1

謝謝@anubhava它的工作 –

0

談到 「WWW」 你可以看到這裏的東西: .htaccess Remove WWW from URL + Directories

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] 
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L] 

的URL,你可以這樣做:

RewriteRule ^some_alias/([0-9]+)/?$ real_page_name.php?id=$1 

在你的情況(你用過「名」 來代替 「restaurants.php」),簡單地說:

RewriteRule ^name/([0-9]+)/?$ restaurants.php?id=$1 

在這裏,您可以得到了很多的解釋和例子: https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/