如何为 seo 友好的 url 编写 htaccess 重写规则
问题描述:
- 我刚接触
.htaccess
. - 我需要一些
URLs
的重写规则. - 我 Google 了一些并应用了,但 URL 没有变化.
我想要:
demo.example.com/section.php?id=1
改为:
demo.example.com/section/sample-section
我试过了
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^section/(d+)*$ ./section.php?id=$1
但没有区别
谢谢.
感谢您的帮助.
答
首先,确保 mod_rewrite 是 enabled 和 htaccess 文件 allowed 在你的 Apache 配置.
First, make sure mod_rewrite is enabled and htaccess files allowed in your Apache configuration.
然后,将此代码放入您的 htaccess(必须在根文件夹中)
Then, put this code in your htaccess (which has to be in root folder)
Options -MultiViews
RewriteEngine On
# redirect "/section.php?id=xxx" to "/section/xxx"
RewriteCond %{THE_REQUEST} s/section.php?id=([0-9]+)s [NC]
RewriteRule ^ /section/%1? [R=301,L]
# internally rewrite "/section/xxx" to "/section.php?id=xxx"
RewriteRule ^section/([0-9]+)$ /section.php?id=$1 [L]