Htaccess重写 - 忽略特定网址结构的规则

时间:2012-03-01 00:36:33

标签: .htaccess

我的网站包含以下htaccess代码:

RewriteEngine on

#
# Externally redirect direct client requests for index page filepaths to “/” URLs
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/\ ]+/)*index\.(php|html?)\ HTTP/
RewriteRule ^(([^/]+/)*)index\.(php|html?)$ http://www.mydomain.com/$1 [R=301,L]
#
# Externally redirect direct client requests to remove file extensions from obsolete inbound links
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/\ ]+/)*([^.\ ]+\.)+(php|html?)\ HTTP
RewriteRule ^(([^/]+/)*[^.]+)\.(php|html?)$ http://www.mydomain.com/$1 [R=301,L]
#
#

# Externally redirect non-canonical hostname requests to canonical hostname
RewriteCond %{HTTP_HOST} !^(www\.mydomain\.com)?$
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
#
#

# Internally rewrite URLs requested with no filetype to php scripts
# unless the requested URL resolves to an existing directory or file.
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /$1.php [L]

它完美运行(对于那些想知道它做什么的人,它强制www域,删除.html / .php扩展名并自动重定向扩展名为无扩展名网址的网址。)

现在好东西:

我在:

创建了一个动态页面
www.mydomain.com/opinion?name=namehere

我希望这个网址被重写并强制执行:

www.mydomain.com/opinion-namehere

我尝试将其添加到上面的htaccess代码中:

RewriteRule ^opinion-([A-Za-z])$ /opinion.php?name=$1 

但是这只是给了我一个“在这台服务器上找不到请求的URL /opinion-namehere.php”。这是有道理的,因为它是通过我的原始代码将该URL重定向到PHP,而该代码不存在。

无论如何我能做到这一点吗?也许忽略该特定网址结构的规则(/opinion.php?name)?

谢谢你!

旧金山

1 个答案:

答案 0 :(得分:0)

添加此规则:

RewriteRule ^opinion-([A-Za-z]+)$ /opinion.php?name=$1 [L]

上面

RewriteCond %{REQUEST_URI} !(\.[^./]+)$

在尝试上述操作之前清除浏览器的缓存。