我正在开发一个项目,所有页面都需要有一个查询字符串假定 id。例如以下页面:
- example.com/alfa.php?id=1
- example.com/beta.php?id=30
- exapmle.com/gamma.php?id=50
我正在使用以下 .htaccess 使我的 URL 更美观。
RewriteEngine On
## BEAUTIFUL URL
DirectoryIndex default.php
Options -Multiviews -Indexes +FollowSymLinks
#RewriteBase /
DirectorySlash off
# remove trailing slash
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
# rewrite /dir/file?query to /dir/file.php?query
RewriteRule ^([\w\/-]+)(\?.*)?$ $1.php$2 [L,T=application/x-httpd-php]
此代码将进行以下转换:
example.com/alfa.php?id=1 to example.com/alfa?id=1
假设 X.php 每次都在变化(X)但 id 总是不变,.htaccess 文件的最佳内容是什么?
我尝试了几种方法,但是最好的方法是什么?
还有一件事,如何将 URL 转换为example.com/alfa/1
ANDexample.com/beta/30