13

I've searched for this question but I only come across really specific answers that seem difficult to tailor to my specific needs.

Let's say the URL I'm attempting to rewrite is this:

http://www.example.org/test.php?whatever=something 

I want to rewrite it so that it appears as this:

http://www.example.org/test/something 

How can I do this?

1

1 Answer 1

29

In order to route a request like /test/something to internally rewrite so that the content at /test.php?whatever=something gets served, you would use these rules in the htaccess file in your document root:

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^/?test/(.*?)/?$ /test.php?whatever=$1 [L] 

And in order to redirect the query string URL to the nicer looking one:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /test\.php\?whatever=([^\&\ ]+) RewriteRule ^/?test\.php$ /test/%1? [L,R=301] 
Sign up to request clarification or add additional context in comments.

2 Comments

If by using above code if someone having issue in trailing query string then they just need to add trailing question mark at end of the last rule, here is a updated code RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /test\.php\?whatever=([^\&\ ]+) RewriteRule ^/?test\.php$ /test/%1? [L,R=301]
i have use the same rules but its not working my url is example.com/directory1/directory2/login.php?id=8 and i want to make it like example.com/directory1/directory2/login/8 please tell me how to fix it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.