Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

A problem with mod_rewrite

5:28 pm on May 8, 2013 (gmt 0)

New User

joined:May 8, 2013
votes: 0


I have been trying several variations to get
my server to redirect - but all seem to fail. :(

This is my url:


Now the data I want to collect in variables is the portion after "huge-big-widgets.example.com/" so in this case
I want "big-widgets" and "p1c9"

Then using the collected variables the server can redirect to:

I have thried this in my .htaccess file:

Options +SymLinksifOwnerMatch
RewriteEngine On

RewriteRule ^[\.0-9-a-z]+/([-a-z]+)/p([0-9]+)pg([0-9]+)\.html$ index.php?p=$2&c=$3&k=$1 [NC,QSA,L]

I also tried :

RewriteCond %{HTTP_HOST} ^[.+].example.com/([-a-z]+)/[.+]$ [NC]
RewriteRule ^[\.0-9,:\/-a-z]+p([0-9]+)c([0-9]+)\.html$ index.php?p=$1&c=$2&k=%1 [NC,QSA,L]

As far as I can see, both should work,
but neither do.

This is the webpage:


If you click on the Category "Big Widgets"

You will see this url:


The htaccess does not properly redirect.

Can anyone see my mistake ?

Thanks .


[edited by: phranque at 1:01 pm (utc) on May 11, 2013]
[edit reason] exemplified hostnames and keywords [/edit]

6:20 pm on May 8, 2013 (gmt 0)

Junior Member

joined:Apr 6, 2013
votes: 0

First, a quick breakdown on why your current rewrite rule doesn't work.


To start off, you're matching two path segments, but in your example URL, there's just one: "monster-bikes". Perhaps you thought you were also matching the domain name? If so, that's not how rewrite rules work. You match on only the path.


The letters in your example URL are "p" and "c", but here you're matching on "p" and "pg".

This should work:

RewriteRule ^([^/]+)/p(\d+)c(\d+)\.html$ index.php?p=$2&c=$3&k=$1 [NC,QSA,L]

means one or more non-slash characters. In other words, this matches a path segment. And
is a shortcut for
7:09 pm on May 8, 2013 (gmt 0)

New User

joined:May 8, 2013
votes: 0


You are correct, I had thought was dealing with the
full url, not just the path.

Thanks again. :)
8:07 pm on May 8, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

joined:Apr 9, 2011
votes: 244

Overlapping previous posts, so this is purely a reply to your first:
Now the data I want to collect in variables is the portion after "www.example.com/" so in this case
I want "monster-bikes" and "p1c9"

Then using the collected variables the server can redirect to:


RewriteRule ^[\.0-9-a-z]+/([-a-z]+)/p([0-9]+)pg([0-9]+)\.html$ index.php?p=$2&c=$3&k=$1 [NC,QSA,L]

Several mistakes here.

Preliminary mistake: Your post says "redirect" but the rule creates a rewrite. That means the user's address bar won't change, but you secretly fetch content from where it lives. As it happens, this is good. You don't want a redirect; you want a rewrite (short pretty URL, long ugly "real" location). Just make sure you've got the terminology straight.

In the RewriteRule itself, the first mistake is the one that prevents the rule from ever matching: the initial

From your example it looks as if this represents the hostname. This isn't used in the body of a RewriteRule-- in fact it's hardly ever used at all in per-directory contexts such as htaccess. Leave it out.

Incidentally, literal periods don't need to be escaped inside grouping brackets. (This is a general Regular Expressions rule, not specific to mod_rewrite.) It won't do any harm; it just isn't necessary.

This part alone

should work. Use an opening anchor with no leading slash. Most Apache installations are happy to let you use \d instead of [0-9] at a savings of three bytes. But if you're not very experienced with Regular Expressions, the byte savings may be outweighed by your personal reaction of "What the ### does \d mean?"

The text of your post says the URL is "p1c2", but the quoted rules have "pg" in place of "c". I assume this is a simple typo.

The target of a rewrite should have a leading / slash. NOT the full domain name! Just the slash.

What's the "QSA" for? Is there any possibility that the original request already contained a query string? If not, leave off the flag. The server has to go looking for a query, and it will never find one. And in the present rule, it seems as if the whole point is that you start out with a nice query-less URL; the query gets added behind the scenes.

Also leave off the [NC] flag. This flag should never be used with rules that create a rewrite instead of a redirect. It doesn't prevent the rule from working; instead it allows the rule to work with an infinitude of different URLs. (Look up Duplicate Content.) It also means your php script has to do extra work, as it now has to regularize all those randomly cased parameters.

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members