如何编写SEO友好的URL的htaccess重写规则

  • 我只是新的.htaccess
  • 我需要一些URLs重写规则。
  • 我谷歌了一些和应用,但没有改变的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 

但没有区别

谢谢。

我会感谢你的帮助。

首先,确保Apacheconfiguration中启用了 mod_rewrite允许 htaccess文件。

然后,把这个代码放在你的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=$1 [L] 
 RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^section/([^/]+)$ /section.php?id=$1 [L] 

这会将example.com/section.php?id=X变成example.com/section/X

我build议将URI存储在数据库中,然后使用section.php?uri =

例如:

example.com/section.php?uri=super-awesome

会变成:

example.com/section/super-awesome