Remove a word from the URL


I have this:

I want to change it to this:

I used .htaccess rewrite rules to get better permalinks for different types of pages on my website: pages, posts, tutorials, authors etc. Here is the code I have:

RewriteRule ^page/([A-Za-z0-9_-]+)$ page.php?page=$1
RewriteRule ^tutorial/([A-Za-z0-9_-]+)$ tutorial.php?tutorial=$1
RewriteRule ^post/([A-Za-z0-9_-]+)$ post.php?post=$1
RewriteRule ^author/([A-Za-z0-9_-]+)$ author.php?author=$1
RewriteRule ^category/([A-Za-z0-9_-]+)$ category.php?category=$1

Is it possible to remove “page”, tutorial", “post” and “category” from the URLs?

If you remove the “page”, “tutorial”, etc from the URLs you will need a new validation method, otherwise $_GET will match the first redirect rule in the list.

You cannot have the same rule ([A-Za-z0-9_-]+)$ for every redirect. You will have to find and set patterns…

For example if page/… values are always numeric … every time a numeric value is sent to the server… redirect to page.php; every time a value with the form “numeric-text” is sent, redirect to tutorial.php. In my opinion this is not a solution as it is impossible to set completely different patterns for all your page types.

If you really need to change the URLs you will have to change the way your script process the $_GET values in one single .php file. So its possible if everything is processed by a single .php page (like index.php) through the $_GET value, and you will need only one redirect rule.

Hi alexandruc thanks for your reply. Your suggestion about processing $_GET in a .php file makes sense to me. Though I have no idea about how to do that.


While Alex is correct about the “generic” matching of the “second level subdirectory” you’re trying to move up one level. When they’re all first level subdirectories, Apache will have no clue which script and key=value pair you want (and, as Alex correctly noted, it will match the first thing that comes along).

Unfortunately, the problem with the $_GET array is irrelevant to the mod_rewrite problem so he introduced a little confusion by looking forward into handling the redirection by your scripts. He’s thinking like a programmer and that’s a GOOD thing!

IMHO, your page/value system appears optimum for what you’re doing.