.htaccess文件mod_rewrite clean url无法正常工作

时间:2012-10-31 23:49:48

标签: .htaccess

所以我想从.htaccess文件中得到通常的东西。 www.example.com/article.php?title=my_title应该替换为www.example.com/article/my_title,但我似乎无法让它工作。截至目前,我可以让.php下降,我怀疑这可能是一些问题。 (我也http://example.com转到http://www.example.com)这是我的.htaccess文件

Options +FollowSymLinks -MultiViews 
rewriteengine on

## this is the piece that is not working
RewriteRule ^title/(.*) article.php?arr=title/$1

rewritecond %{HTTP_HOST} ^example.com$
rewriterule ^example\/(.*)$ "http\:\/\/www\.example\.com\/$1" [R=301]

RewriteBase /

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

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

## Error document (needs to have a 404 page created)
ErrorDocument 404 http://www.example.com/

1 个答案:

答案 0 :(得分:1)

您需要在之前添加这些特定的路由规则通常适用于php文件的任何规则。所以在你的rewriteengine on下,添加:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/article\.php\?title=([^&\ ]+)
RewriteRule ^ /article/%1? [L,R=301]

RewriteRule ^/?article/(.*)$ /article.php?title=$1 [L]

然后是你的其他规则。