.htaccess:为/*.php工作的RewriteRules不适用于/index.php

时间:2014-08-05 10:29:20

标签: regex apache .htaccess mod-rewrite

我正在使用mod-rewrite来使我的网址更漂亮。除了index.php之外,一切正常。

每个php文件都获取该语言的参数,例如contact.php?lang=de。该文件可由/contact/de调用。由于en是默认语言,因此不应出现在网址中:/contact。这工作正常。同样适用于ìndex.php?lang=...,区别在于英语(默认)版本为/(而不是/index),德语版本为/index/de(与联系)。

Options +FollowSymLinks
RewriteEngine on

# No trailing slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^\.localhost$ [NC]
RewriteRule ^(.+)/$ htt p://%{HTTP_HOST}/$1 [R=301,L]

RewriteBase /
RewriteRule contact$        contact.php?lang=en [L]     # default language
RewriteRule contact/(.*)$   /contact.php?lang=$1 [L]    # any other language

# default language (don't show any thing in the URL)
RewriteRule index/en$       / [L,R=301]

# requesting /index.php, one should be forwarded to /
RewriteRule index.php$      / [L,R=301]

RewriteRule /$              index.php?lang=en [L]       # default language
RewriteRule index/(.*)$     /index.php?lang=$1 [L]      # any other language

http://domain/index/en工作正常,即网址转发到http://domain/http://domain/index/de转发至http://domain/?lang=de。这是正确的,但不应更改URL。即使我在htaccess文件中删除了R=301的任何RewriteRule,URL仍然在变化。我必须弄清楚为什么会这样。我正在做同样适用于/contact的工作。 index.php有没有隐含的规则?

1 个答案:

答案 0 :(得分:1)

您可以使用:

Options +FollowSymLinks
RewriteEngine on

# No trailing slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^\.localhost$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L,NE]

RewriteBase /
RewriteRule contact$        contact.php?lang=en [L,QSA]     # default language
RewriteRule contact/([^/]+)/?$   contact.php?lang=$1 [L,QSA]    # any other language

# default language (don't show any thing in the URL)
RewriteRule index/en$       / [L,R=301]

# requesting /index.php, one should be forwarded to /
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=301,NC,NE]

RewriteRule ^/?$             index.php?lang=en [L,QSA]       # default language
RewriteRule ^index/([^/]+)/?$     /index.php?lang=$1 [L,QSA]      # any other language