URL Rewrite question
I know how to rewrite URLs like the following:
What I want to achieve is to remove "post" and "category" from the URLs and have the following:
RewriteRule ^post/([a-z0-9-]+)/$ post.php?post=$1
RewriteRule ^category/([a-z0-9-]+)/$ category.php?category=$1
Is this possible with .htaccess or do I need to have some sort of PHP solution?
Thanks for any ideas.
Sorry, you can't get there from here.
Consider how you expect Apache to determine which of your RewriteRules to implement if it matches ([a-z0-9-]+).
I can assure you that it will only match the first one it encounters and the second will never be matched. The reason for this is the difference between your two redirection is post vs category and you MUST provide that distinction in SOME manner (your desire to strip post and category removes the critical distinction). Perhaps you can conceive of another distinctive marking which Apache can use to tell the two apart.
Wait! Do the "post-1" and "category-1" entries mean that you're using post or category followed by a hyphen and digits? If so, that's solved your distinction problem as you can use:
That will pick-up either post or category from the URI, strip the -, then capture the digit(s) and redirect to the appropriate script with the correct query string.
RewriteRule ^(post|category)-(\d+)$ $1.php?$1=$2 [L]
WARNING: You should already have discovered that using dir/data/ will cause your post and category scripts to be missing their css, js, jpg, etc. supporting files. That is because you've changed the directory level perceived by the browsers. I would recommend that you visit the mod_rewrite tutorial linked in my signature for the pair of "cure" options but I would also recommend that you not masquerade the data (post-1 and category-1) as a directory, too, as that will make for more typo problems if your URI is ever entered by hand.
Thanks for your input. I know what I am trying to achieve is possible with or without .htaccess, I just don't know how to do it yet.
"post-1" and "category-1" were just samples, I mean post or category are not followed by hyphens and digits. They are just post names and category names.
But, I learned something from your rewrite rule, thanks for it.
When I was using WordPress, I had such URLs without additional words (category, etc.) but I couldn't figure out how it was doing it.
I found an article which explains some sort of PHP rewriting, maybe I can find my answer there.