Rewrite to


At web site “” I found this:

Now lets assume you have a blog and you have a page where you display articles from a specific category and your url looks like You could rewrite it to Now you don’t want to have to do this for all categories because if you add a new category you would have to write a new line.

RewriteRule ^category/([0-9]+)$ category.php?id=$1

If I just want instead do I just miss out the “^category/”? Or would this not work?

And I will be using characters rather than numbers. The end bit of my original URL will be product.php?productname=iphone. So the end bit of the rewrite will need to be “product=$A”??? Rather than “id=$1”??


…I have just found another coding at “” which is:

The Apache rewrite engine is mainly used to turn dynamic url’s such as “” into static and user friendly url’s such as “

RewriteEngine on
RewriteRule ^product/([^/\.]+)/?$ product.php?id=$1 [L]

Why is there more than 1 method?? And which is the best method to use?? What is the difference?


The difference between the rules is that the first one uses ([0-9]+), which is "any character between 0 and 9 (i.e. 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9, indicated by [0-9]) one or more time (indicated by +), so basically this matches any number. This number is than saved (indicated by the parentheses) and can then be used as $1 in the right side.
The variables in a RewriteRule range from $1 (the first set of parentheses) to $9 (the ninth set of parentheses) and $A does not exist. In that rule you should still use $1.
However, since the rule matches only numbers and you need to match titles with alpha characters the rule is useless to you.

Moving on the second rule, that contains ([^/\.]+). That is “Everything except (indicated by the ^ in the character definition) a slash, backslash or dot.” This could be what you want if your titles never contain any of those characters.

However, more specific to your case would be ([a-zA-Z0-9-]+) which is “any character uppercase or lowercase, any number, or a dash, one or more times”.

Does that make sense?

Hi ScallioXTX,

I am having a problem now with the ‘Get’ function because my new links do not specify the variable name to get?!?!

The code I am using is:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.sitename\$ [NC]
RewriteRule .?{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^$
RewriteRule ^([a-zA-Z0-9-]+)$ product.php?subcategory1=$1
RewriteRule ^([a-zA-Z0-9-]+)/$ product.php?subcategory1=$1

Obviously the raw link is 'product.php?subcategory1=1 and the new links in my web site are now simply product/1.

The problem now is subcategory1=1 is not being recognised in my PHP code. I have a line that starts “if(isset($_GET[‘subcategory1’]))” etc. but this is not being recognised with my new link. Is this a common problem… can I still use the ‘Get’ function?? What is wrong? Any ideas?


Before I answer your question, let me first point out that you don’t need the second RewriteCond. Also, you can easily combine those two rules you have there.

RewriteRule ^([a-zA-Z0-9-]+)/[COLOR="Red"]?[/COLOR]$ product.php?subcategory1=$1

Although it would be even better to decide if you want to have URLs with the slashes or without and stick to it. That way you don’t have two URLs with the same content; search engines don’t like that.

On to your question, I don’t see how the rule you give should work on product/1 since the word product isn’t even in the rule.

Could we please go back to the beginning where you explain what it is you actually want to do? :slight_smile:

Hi ScallioXTX,

Before I explain what I want let me clarify something. I would like to have links like

but do search engines penalise the site for not having “.htm” at the end. If the links had .htm at the end the search engines would just think it was a html page. If I miss this out do search engines think ‘this is not an html page and is obviously something to do with php so I will penalise the site’ — if this is the case I’d like to include the .htm.

Now onto what I want to do:

I gave a subcategory example previously but if you think of the links for product pages instead I can probably apply the same idea to my subcategories. So, if I use PHP the links look like

and then the get function is used to populate the template page and generate the correct navigation bar.

and I want to instead have the link

Like I said, if .htm works better with search engines then instead I want

Hope this is clear,


In that case it would just be

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\\.sitename\\$ [NC]
RewriteRule .?{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9-]+)$ product.php?productname=$1

The two RewriteConds are in there to avoid existing files and directories from being rewritten, which could cause all kinds of havoc.

I’m no SEO expert so I’m not entirely sure but as far as I know search really don’t care much whether you have .html or not. If you really want to know I suggest you search the SEO forum here on SitePoint; I’m sure you’re not the first person to ask :slight_smile:

I have tried the code you provided but it is not working. I have tried links to both

and these should both generate the same navigation bar. The isset function is used to find productname and if this is present other products from this category are displayed in the navigation bar. I can only think that this ‘get’ function is not working properly but I know it should be possible to do. Any ideas what I may be doing wrong. I did wonder whether the “REQUEST_FILENAME” should be replaced with a filename as I am not sure what this 2 lines of code do (that you added).

Any ideas?


No the REQUEST_FILENAME is fine as is, and prevents Apache from rewriting URLs to existing files and directories.
What output do you see when you put var_dump($_GET); in your PHP and go to [noparse][/noparse] ?

I think I know what the problem is… The best way to explain the problem is if I add a link to a web page say called test.php and click this link (, it just loads the page I have just clicked the link at (test.php) but the URL reads

Not sure why or how this is doing this though? Something is going badly wrong?!!

Any ideas??


can you put var_dump($_SERVER); in test.php, do what you described in your last post, and post (or PM) the result here please?

Hi ScallioXTX,

I have found the problem which is probably an obvious mistake from your point of view.

The .htaccess redirect is designed to go to

The problem was I had a PHP file I was messing about with called iphone.php on the server.

And, as you probably know when you redirect to a more friendly URL rather than go to that page it takes the viewer to the page with that name. Perhaps you know more about this problem than me??

Anyhow it is solved now and I know not to save any pages with the name (and a .php extension) used in a Mod ReWrite!!


:redhot: MultiViews :redhot:

Stupid, stupid, MultiViews, I hate that option!

Put this in the .htaccess

Options -MultiViews

And it will not load iphone.php when you request /iphone but just obey your RewriteRules

If your rewrites are not working, you may not have Rewrite abilities in your app. Check your httpd.conf (or apache2.conf) file to check that you have AllowOverride All set for your site. Also check that you have mod_rewrite installed in your mods.

Edit: nevermind, missed the last couple posts… problem seems resolved.

How about images on a web site? I have heard that I can prevent people using the images using .htaccess. Is this worth doing? Is there any other reason why I would want to Mod Rewrite where the images are accessed??


On the topic of prevent people using you’re image (which is called hotlinking):

Whether it’s worth doing is up to you to decide, we can’t decide that for you :slight_smile:
If you do want to do it, here’s a nice tut Stop Hotlinking with htaccess. Test your Image Hotlink Protection with our Hotlink Checker from


Hi. Can you please just explain in words how .htaccess works. Before I knew how to code it, I thought it was like a 301 redirect. For example:

  1. I add links like
  2. The .htaccess file is accessed and then it is redirected to

However, I now know that I should include links in my web site like “appleiphone4” and then this goes to So this suggests that whenever someone click a link the user first loads .htaccess to determine what link (or what page) they will end up at. If this is true, then does this cause problems with search engines (having to access the .htaccess file first) and does it increase download time of the page, as users are first going to a .htaccess file!?


No, the user doesn’t load the .htaccess; the user doesn’t even have access to that file.

Apache (the web server) reads the file whenever a request is made and processes all the rules. If any match it will apply them.

It’s a like an Apache config file but specific for your website (and you can’t set all config directives in it, some options are reserved to httpd.conf).

Does that make sense?


I have never heard of httpd.conf. Is is worth me reading up about this or not?

Once the new site is ready to go live i will be applying a 301 redirect on all old pages (as suggested by Google). Will I need to redirect every page individually or is there a way to say “any page that used to end with .htm now has it missing” ie. becomes

I am prepared to add rules for each and every page (about 2,000 pages) if I need to, to ensure old content becomes ranked identically replaced by the new content. What is the best method - do you know? And another question following from that… am I right to assume Search Engines will not actually see the old pages at all (although they will all remain online indefinitely) because the .htaccess file steers Search Engines away from the old pages…which makes me sceptical about how the new pages with the 301 redirect will rank identically to the old pages (as suggested by Google).

I understand what you said about the server accessing the file but search engines access the server, so surely they access the .htaccess file too??


No one (I repeat: no one) accesses the .htaccess file but Apache. Not your visitors, not the search engines, not php, no one. Absolutely no one.

Apache redirects search engines to the new location if they request a URL needs to be redirected.

As for the .htm, that’s pretty simple; just create a rule that strips the .htm from the requested URL and 301 redirect

RewriteEngine On
RewriteRule ^(.*)\\.htm$ $1 [L,R=301]

And indeed if you have that set up while .htm files still exists google will not index those (the .htm files) anymore. A 301 redirect is like saying “Resource A has been replaced by resource B. Please forget about resource A completely and only look at resource B from now on as A’s replacement.”
This might take a while btw, google doesn’t do this instantaneously.