如何编写 seo 友好的 htaccess 重写规则 url
How to write htaccess rewrite rule for seo friendly url
- 我刚接触
.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=
但没有区别
谢谢。
非常感谢你的帮助。
首先,确保 mod_rewrite 已 启用 并且 htaccess 文件 已允许 在您的 Apache 配置中。
然后,将此代码放入您的 htaccess(必须在根文件夹中)
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= [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^section/([^/]+)$ /section.php?id= [L]
这会将 example.com/section.php?id=X
变为 example.com/section/X
我建议将 URI 存储在数据库中,然后使用 section.php?uri=
例如:
example.com/section.php?uri=super-awesome
会变成:
example.com/section/super-awesome
- 我刚接触
.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=
但没有区别
谢谢。
非常感谢你的帮助。
首先,确保 mod_rewrite 已 启用 并且 htaccess 文件 已允许 在您的 Apache 配置中。
然后,将此代码放入您的 htaccess(必须在根文件夹中)
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= [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^section/([^/]+)$ /section.php?id= [L]
这会将 example.com/section.php?id=X
变为 example.com/section/X
我建议将 URI 存储在数据库中,然后使用 section.php?uri=
例如:
example.com/section.php?uri=super-awesome
会变成:
example.com/section/super-awesome