ow6twa
Last Updated: February 25, 2016
·
18.3K
· anthonylevings
Speechrocket4 square

Super simple .htaccess rewrite to remove PHP file extensions and query strings from URLs

The first step is to create a .htaccess file, which should contain the following code and be saved in the same folder as you intend to save the index.php file

RewriteEngine on

RewriteRule ^([a-zA-Z0-9]+|)/?$ index.php?name=$1
# Handle requests for "index.php"

Now create the index.php file and insert the following

<?php echo $_GET['name']; ?>

Now it's time to test

You'll need to be running something like MAMP or have this code on your server. Type the first part of the URL so that you are pointing the web browser at the folder that contains the two files you created. Next type after the forward slash a word, e.g.

http://localhost:8888/rewrite/myname

Note: Here my files were saved in a folder called rewrite (and I was using MAMP).

It's all very well having fixed positions in the URL relating to query terms, but sometimes we'll want the added flexibility of also accepting regular query strings, e.g.

http://localhost:8888/rewrite/myname?age=39

If we tried processing this at present, the query string would be ignored. To stop this from happening we need to add [QSA], which appends any query string from the original request URL to any query string created in the rewrite target.

RewriteRule ^([a-zA-Z0-9]+|)/?$ index.php?name=$1 [QSA]
# Handle requests for "index.php"

Now replace the index.php code with the following:

<?php 
if (isset($_GET['name'])) echo "Hello ".$_GET['name'].", ";
if (isset($_GET['age'])) echo "you look really young for ".$_GET['age'];
?>

Another handy thing to know is that if we add [NC] to the end of the RewriteRule then it becomes case insensitive:

RewriteEngine on

RewriteRule ^([a-z0-9]+|)/?$ index.php?name=$1 [QSA,NC]
# Handles requests for "index.php"

For further discussion of ReWrite Rule flags (e.g. QSA, NC), see http://httpd.apache.org/docs/current/rewrite/flags.html

I discuss at further length rewriting here: http://sketchytech.blogspot.co.uk/2013/01/super-simple-htaccess-rewrite-to-create.html

Say Thanks
Respond

11 Responses
Add your response

5846

Thanks for this Anthony, I've been looking to do some rewriting for project at work and been stuck with it all day - just had a read through of your blog post and that's given me enough to do what I needed! Great stuff!

over 1 year ago ·
5864
Speechrocket4 square

@harryfinn I'm glad that it helped you out, and thanks for taking the time to comment.

over 1 year ago ·
8844
0 cv5yq4twpoclhqzz8ok quaxpw3v2qzz8zliqu1miez51k74apvkhrbb ite71mn jbcwptxbw7e

I'm new here and I got this htaccess code to redirect:
RewriteRule ^viewtopic.php http://www.sub.myurl.com/? [L,R=301]
for ex:
http://www.sub.myurl.com/viewtopic.php?product_names
Output when redirected:
http://www.sub.myurl.com/?product_names
getting output link as I needed but the link is not working properly(not linking to respected page) please help me.

over 1 year ago ·
8850
Speechrocket4 square

@prakash j you shouldn't need all the http://www.sub.myurl.com because you'll just be redirecting from either the base folder or a subfolder.

The first part - ^([a-zA-Z0-9]+|)/?$ - is the regex that captures the input text. We then reference the text captured by the regex with $1 and send it to index.php using the command index.php?name=$1.

In your example you've missed out various bits, it should look more like this:

RewriteEngine on
RewriteRule ^([a-zA-Z0-9]+|)/?$ viewtopic.php?product_names=$1

and this should be saved in a .htaccess file in the base folder of your site: http://www.sub.myurl.com/

You then will be able to capture the product name using:

<?php echo $_GET['product_names']; ?>

in the viewtopic.php file when the URL is something like this: http://www.sub.myurl.com/catfood

over 1 year ago ·
8865
0 cv5yq4twpoclhqzz8ok quaxpw3v2qzz8zliqu1miez51k74apvkhrbb ite71mn jbcwptxbw7e

@anthonylevings : Thanks for your kind reply I am happy bit, the above code is not working, here is the original link I have to rewrite:
http://tresbizz.com/teamfront.php?Adobe-Acrobat-XI-Standard
there is no any symbol("=") or nothing. I just need this link to redirect like:
http://tresbizz.com/?Adobe-Acrobat-XI-Standard
or
http://tresbizz.com/Adobe-Acrobat-XI-Standard
fine with these both np and I don't like to edit any of my php files, please help me out

over 1 year ago ·
8866
Speechrocket4 square

@prakash j: You seem to be talking about a redirect rather than a rewrite, I've not implemented any myself but I think you'd need to do something like this in your .htaccess file

## 301 Redirect Entire Directory
RedirectMatch 301 http://tresbizz.com/teamfront.php?(.*) http://tresbizz.com/$1
over 1 year ago ·
8868
0 cv5yq4twpoclhqzz8ok quaxpw3v2qzz8zliqu1miez51k74apvkhrbb ite71mn jbcwptxbw7e

Thanks for helping Anthony, I iust tried to copy the above code and paste into htaccess, I do not see any changes in url:(

over 1 year ago ·
8869
Speechrocket4 square

Try this instead:

## 301 Redirect Entire Directory
RedirectMatch 301 teamfront.php?(.*) /$1
over 1 year ago ·
8870
0 cv5yq4twpoclhqzz8ok quaxpw3v2qzz8zliqu1miez51k74apvkhrbb ite71mn jbcwptxbw7e

YES, It is working fine but not linking to the respective page, linking to home page instead.
Thanks,

over 1 year ago ·
8872
Speechrocket4 square

You might need to Redirect and Rewrite (and in the right order), the best thing to do is to look at the modrewrite documentation: http://httpd.apache.org/docs/2.2/mod/modrewrite.html and http://httpd.apache.org/docs/2.2/rewrite/remapping.html

over 1 year ago ·
9203
097c37737def7ca45d79283bcccb8046

Very good and minimal, thanks

over 1 year ago ·