首页 > 解决方案 > url RewriteEngine 未按预期工作

问题描述

我正在尝试转换:

http://localhost/mysite/blog.php?slug=post-by-paritosh

至 :

http://localhost/mysite/blog/post-by-paritosh

为此,我使用具有以下规则的 .htaccess 文件

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?blog/(.*?)/?$ /blog.php?slug=$1 [L]

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

http://www.htaccesscheck.com/检查语法后,我发现它没问题,但重写没有按预期工作。

标签: .htaccessurlmod-rewriteurl-rewriting

解决方案


你可以试试这个 .htaccess 里面的tgconnectmediaservice/目录:

Options -MultiViews
RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+(tgconnectmediaservice/blog)\.php\?slug=([^&\s]+)\s [NC]
RewriteRule ^ /%1/%2? [L,R=301,NE]

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

推荐阅读