如何为 seo 友好的 url 编写 htaccess 重写规则 [英] How to write htaccess rewrite rule for seo friendly url
本文介绍了如何为 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]
这篇关于如何为 seo 友好的 url 编写 htaccess 重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文