RewriteEngine隐藏扩展和GET

时间:2013-05-18 16:40:14

标签: php .htaccess mod-rewrite url-rewriting

如何重写此网址?

http://website.com/file.php?lang=enhttp://website.com/file/en

文件名和参数可以改变,我发现很多主题我没有找到扩展和GET参数的组合

更新:我现在正在努力隐藏php扩展,现在如何添加参数

# Turn mod_rewrite on
RewriteEngine On

# hide .php extension
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L]

# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]

2 个答案:

答案 0 :(得分:1)

从代码和评论我可以说这是我的答案之一:P

为了获得这个新的http://website.com/file.php?lang=en to http://website.com/file/en,你的.htaccess是这样的:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

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

# hide .php extension
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L]

# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

答案 1 :(得分:0)

尝试此配置

RewriteEngine On
RewriteRule ^file/([a-zA-Z0-9_-]+)$ file.php?lang=$1