我有一个需要重新格式化的网址。 example.com/?article_category=lifestyle
我希望将其格式化为:example.com/lifestyle。但是,当我尝试使用我的.htaccess文件重新格式化它时,它不起作用。我的.htaccess文件与我的文件位于同一目录中。文件名是index.php。
这是我提议的重写规则:
RewriteRule ^([\w-]+)/?$ index.php?article_category=$1 [QSA,L]
我不知道为什么这不起作用。我做错了什么?
答案 0 :(得分:1)
实用文档:RewriteCond Directive& RewriteRule Directive
RewriteCond %{QUERY_STRING} article_category=([a-z]+)
RewriteRule ^/?$ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]+) index.php?article_category=$1 [QSA,L]
第1行:检查包含article_category=([a-z]+)
,article_category=lifestyle
的查询字符串是否匹配
第2行:如果网址为example.com
或example.com/
,则永久重定向到/lifestyle
,%1
是RewriteCond反向引用,分组部分(在括号{{1}中) })模式
第3/4行:如果URL不是常规文件和目录
第5行:如果网址以字母开头,请将网址重写为([a-z]+)