mode rewrite general rule to hide dynamic url elements?
Here's what I have:
www.name.com/category.php?title=111-hello-kitty
This is wha开发者_如何转开发t I would like:
www.name.com/category/111-hello-kitty
Also is it possible to hide the number part in htaccess so it looks like this:
www.name.com/category/hello-kitty
Here's my current code:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.+)\.php$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^([^/.]+)$ /?title=$1
Obviously, the last line is not working.
Can I call in general to have all ?anything=
to be replaced by a / like the example above?
My thoughts:
- Hiding the number will require some SQL changes. This is because you no longer have the ID available to you, now you have to query the database to get it.
- I would use
www.name.com/category/111/hello-kitty/
for my URL. The reason is that this way you can systematically parse each portion. Notice that this is also what StackOverflow is using. The following RewriteRule assumes thatcategory
and the section forhello-kitty
contains only alphanumeric symbols and a dash (a common practice):
RewriteRule ^([A-Za-z0-9-]+)/([0-9]+)/([A-Za-z0-9-]+)/?$ /$1.php?title=$2-$3
#although I'd recommend that you really do this:
RewriteRule ^([A-Za-z0-9-]+)/([0-9]+)/[A-Za-z0-9-]+/?$ /$1.php?id=$2
Do something like this:
RewriteRule ^category(/.+)?$ /category.php [L]
精彩评论