开发者

Which mod_rewrite rule can remove index.php and php extensions (from another existing rule)?

I use the following pattern:

http://example/index.php?lang=en
http://example/index.php?lang=es
http://example/index.php?lang=zh-tw
http://example/index.php?lang=zh-cn

And I use the following mod_write rule:

RewriteEngine on
RewriteRule ^([a-z]{2}(-[A-Z]{2})?)/(.*) $3?lang=$1 [L,QSA]

With that rule I can use the following URLs which redirect me to the URLs above (the original ones):

http://example/en/index.php
http://example/es/index.php
http://example/zh-cn/index.php
ht开发者_Go百科tp://example/zh-tw/index.php

I wish I could just type:

http://example/en
http://example/es
http://example/zh-cn
http://example/zh-tw

To access the index pages...

and type this:

http://example/en/thanks
http://example/es/thanks
http://example/zh-cn/thanks
http://example/zh-tw/thanks

to enter a thanks.php file I have.

Any suggestions to get that result?

(I think this should be quite common. Almost every professional website has pages in this fashion: example/section).


If I understood what you were looking for correctly, the following should do what you want (my rules make the assumption that you're using the root of a domain):

RewriteEngine on

# Go to index.php by default if nothing is specified after the language
RewriteCond %{REQUEST_URI} ^/[a-z]{2}(-[A-Za-z]{2})?/?$
RewriteRule ([^/]*)/?$ index.php?lang=$1

# Rewrite domain.com/lang/index to domain.com/lang
RewriteRule ^([a-z]{2}(-[A-Z]{2})?)/index(\.php)?$ $1 [R,QSA]

# Allow pages to be specified without the .php extension
# The RewriteCond tells mod_rewrite to not rewrite if the request is a real file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]{2}(-[A-Z]{2})?)/(.*)(\.php)? $3.php?lang=$1 [L,QSA]

If you are not at the root of the domain (ie. your URLs are domain.com/subdomain/en/index), you need to specify RewriteBase /subdomain and modify your RewriteCond slightly (you'd put the rules in a .htaccess file in the subdirectory):

RewriteCond %{REQUEST_URI} ^/subdomain/[a-z]{2}(-[A-Za-z]{2})?/?$

If you're still having trouble getting your CSS to load, try using absolute paths. The relative path will apply after the redirect.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜