Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

.htaccess rewrite problem



11:12 am on Mar 21, 2008 (gmt 0)

10+ Year Member

Can someone help me with this regular expression problem that i am having?

I use .htaccess to rewrite my URL's

RewriteEngine On
RewriteRule -Jewellery-([a-z,A-Z,0-9,_]*)\.html /index.php?m=productDetail&p=$1

This finds any product which ends with "-jewellery-100.html", the number 100 can be any number and is my product ID. This works fine, only problem is i have renamed a page and need to do a 301 to the new page, so i now have.

#REDIRECTS -------
redirectpermanent /test/OLD-PAGE-Jewellery-100.html http://www.example.com/MY-NEW-PRODUCT-Jewellery-100.html
RewriteEngine On

RewriteRule -Jewellery-([a-z,A-Z,0-9,_]*)\.html /index.php?m=productDetail&p=$1

This then redirects, but because of the rewrite rule below it passes through a querystring, like so.


I need to not have the querystring added by the rewrite, so i though something like

RewriteRule ([^OLD\-PAGE]+)-Jewellery-([a-z,A-Z,0-9,_]*)\.html /index.php?m=productDetail&p=$1

But i cannot get it to work as i would like - Someways it redirects but then doesnt recognise the rewrite rule so pages arent found. I have tried a variety of ways and different expressions - but they arent my strong point.

Please can someone help - i am pulling my hair out....


3:29 pm on Mar 21, 2008 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

First, your regular-expressions patterns don't look right. Unless you want to allow commas in the product number or blank product numbers, the "[a-z,A-Z,0-9,_]*" pattern should be "[a-zA-Z0-9_]+"

The regular-expressions pattern in your second RewriteRule is (sorry) unrecognizable...

Second, do not mix mod_rewrite and mod_alias directives (such as "RedirectPermanent") unless you know that the URLs to be handled by the two directives are mutually-exclusive and will never interact. In your case here, that is not true -- We can be sure that they will interact! So use mod_rewrite for both functions, and put the external redirect first in order to avoid exposing your query string.

Third, bear in mind that we are doing two very different things here: We are externally redirecting an old URL, and we are internally rewriting static URLs to a dynamic script path. An external redirect informs and requires the cooperation of the client (browser or search engine robot), while an internal rewrite happens completely inside the server and is not (normally) visible to the world.

All-in all, I think you'll be much happier with these two rules in this order:

RewriteRule OLD-PAGE-Jewellery-100\.html$ http://www.example.com/MY-NEW-PRODUCT-Jewellery-100.html [R=301,L]
RewriteRule -Jewellery-([A-Za-z0-9_]+)\.html$ /index.php?m=productDetail&p=$1 [L]

See the regular-expressions tutorial cited in our forum charter, and another one in our forum library (links to charter and library at top of this page).



9:11 pm on Mar 21, 2008 (gmt 0)

10+ Year Member

Thank you very much Jim. That has worked perfectly.

I am pretty new to using regex and modrewrite so sorry if my first post was well off the mark - i have been using the same regex to pass variables using modrewrite for a couple of months now and this is the first time i have become stuck with it.

Thanks alot for your help and i will deffinitely do some reading up on the regex stuff in the forum library.



Featured Threads

Hot Threads This Week

Hot Threads This Month