2016-10-03 48 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/