2

Lets say I want users to be able to type this url in:

www.website.com/blog/2453/I-gained-0.1%-more-scripting-knowledge-!

I'm trying to include title information in the url for seo benefits.

I also want to include an id for my query. Effectively I want to pick up the id and ignore the title stuff that comes after, bearing in mind its user generated text so could contain any special characters in it.

How can I write a .htaccess rewrite rule so that the server reads it as the following with the appropriate GET data:

www.website.com/blog.php?id=2453

This is what I have tried but frankly I am way out of my depth here:

RewriteRule ^blog/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$  blog.php?id=$1 [NC,L]
4

1 回答 1

1

.除了 URL 中的、%!字符之外,您使用的重写规则应该可以工作。这些%字符在 URL 中使用是不安全的,因为它在 URL 语法中具有特殊含义。我也不会使用感叹号。

如果 ID 始终为数字,请使用([0-9]+)而不是([A-Za-z0-9-]+).

试试这个网址:

www.website.com/blog/2453/I-gained-0.1-more-scripting-knowledge

有了这个规则:

RewriteRule ^blog/([0-9]+)/[A-Za-z0-9\-\.]+/?$  blog.php?id=$1 [NC,L]
于 2013-03-19T18:09:15.313 回答