htaccess和mod_rewrite包含第二个参数和现有文件夹

时间:2013-11-10 10:57:36

标签: .htaccess mod-rewrite url-rewriting

我相信已经多次回答,但我无法找到答案。

我正在努力让友好的网址工作,但无法写出重写规则。

我需要将 www.mysite.com/123 重写为 www.mysite.com/index.php?page=123

还有一个页面: www.mysite.com/123/abc www.mysite.com/index.php?page=123&secondParam=abc

此外,我希望能够通过www.mysite.com/something链接到以下页面:www.mysite.com/something/index.php,并且不要将其重写为www.mysite.com/index.php? page =如果服务器上存在某个文件夹的话。

感谢。

1 个答案:

答案 0 :(得分:1)

试试这个。 以“#”开头的行是注释,仅用于记录命令。

<IfModule mod_rewrite.c>
# calls to initiate our requests Apache's Rewrite Module (you need this only once)
RewriteEngine on

# "If the requests does not point to an existing file"
RewriteCond %{REQUEST_FILENAME} !-f
# "If the requests does not point to an existing folder"
RewriteCond %{REQUEST_FILENAME} !-d
# "If the request contains a "2 subfolders" structure, redirects it silently to ..."
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?page=$1&secondParam=$2 [NC,QSA,L]
RewriteRule ^([^/]+)/?$ index.php?page=$1 [NC,QSA,L]
</IfModule>

试试最后一行的这个版本:

RewriteRule ^(.*)/(.*)/?$ index.php?page=$1&secondParam=$2 [NC,QSA,L]

RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?page=$1&secondParam=$2 [NC,QSA,L]