所以我使用.htaccess来解析所有非www请求到www。域名,但我正在使用的代码生成一个url,即使是直接请求也会显示index.php,即输入example.co.uk - >加载www.example.co.uk/index.php。
下面是代码:
RewriteCond %{HTTP_HOST} !^www\.site\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.site.co.uk/$1 [R=301,L]
完整.htaccess
RewriteEngine On
RewriteBase /
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} !^www\.site\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.site.co.uk/$1 [R=301,L]
AddHandler php55-script .php .php5
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
#2 Permanent URL redirect
Redirect 301 /index.html http://site.co.uk/index.php
#3 Permanent URL redirect
Redirect 301 /lizards.html http://site.co.uk/stocklist/lizards.php
#4 Permanent URL redirect
Redirect 301 /snakes.html http://site.co.uk/stocklist/snakes.php
#5 Permanent URL redirect
Redirect 301 /amphibians.html http://site.co.uk/stocklist/amphibians.php
#6 Permanent URL redirect
Redirect 301 /misc.html http://site.co.uk/stocklist/misc.php
#7 Permanent URL redirect
Redirect 301 /tortoisesandturtles.html http://site.co.uk/stocklist/tortoisesandturtles.php
答案 0 :(得分:1)
您需要确保将此www
规则保留在其他规则之前。
或者您可以稍微修改此规则:
# for index.php OR home
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(index\.php)?$ http://www.reptilesplus.co.uk/ [R=301,L]
# for rest
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.+)$ http://www.reptilesplus.co.uk/$1 [R=301,L]
答案 1 :(得分:0)
是的,可以通过将index\.php
添加到未被复制到url的第一个参数的一部分来实现。您可以使用?
使其成为可选项,这使前一个字符/组匹配0或1次。我们可以使用[OR]
标志来检查网址是否不以www开头。或者index.php出现在网址中。
以下(未经测试的)代码应该可以解决这个问题
RewriteCond %{HTTP_HOST} !^www\.reptilesplus\.co\.uk$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/index\.php
RewriteRule ^(index\.php/?)?(.*)$ http://www.reptilesplus.co.uk/$2 [R=301,L]
此应该发生:
http://example.com/index.php/1 --> http://www.example.com/1
http://example.com/1 --> http://www.example.com/1
http://www.example.com/index.php/1 --> http://www.example.com/1
http://www.example.com/1 --> Doesn't match