.htaccess重写友好的URL

Oak*_*esy 6 php regex apache .htaccess mod-rewrite

我不确定这是否可行,但我有以下网址,我想让它更友好:

http://www.myurl.com/content.php?id=13089093057550&slug=this-is-a-nice-url
Run Code Online (Sandbox Code Playgroud)

我希望它看起来像这样:

http://www.myurl.com/this-is-a-nice-url
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?

anu*_*ava 7

您需要将id传递给PHP代码才能显示我认为的内容.例如,查看此问题的Stack Overflow URL:http://stackoverflow.com/questions/6520671/htaccess-rewrite-friendly-urls它在哪里传递idslug.所以你可以拥有友好的URL,如:

http://www.myurl.com/13089093057550/this-is-a-nice-url
Run Code Online (Sandbox Code Playgroud)

如果你决定按我的建议去做,那么这里是你需要的代码.htaccess:

Options +FollowSymlinks -MultiViews
RewriteEngine On

RewriteRule ^([0-9]*)/(.*)/?$ /content.php?id=$1&slug=$2 [L,QSA]
Run Code Online (Sandbox Code Playgroud)


Mic*_*ski 2

您可以将 the 放入slug查询字符串中,但是如果不提供 theid某处,Apache 就不可能提供它。希望您有办法仅使用 URL slug 参数从数据库中获取 id。

RewriteEngine On
# We don't know the id
RewriteRule (.*)$ /content.php?slug=$1 [L,QSA]
Run Code Online (Sandbox Code Playgroud)