Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

mod rewrite rule ignored

7:37 pm on Jan 27, 2014 (gmt 0)

New User

5+ Year Member

joined:June 20, 2007
votes: 0

Dear friends,

I'm new to mod_rewrite rules but I think I understood how it works. I'm testing the following rule for the following URL:

RewriteEngine On
RewriteRule ^index/pg/([a-z])/?$ index.php/pg=$1


But accessing this URL doesn't makes the server rewrite it to


Is the rule wrong? The hosting support only says that this issues are not scope of their support...

Thank you,
D. Duarte

[edited by: phranque at 12:48 am (utc) on Jan 28, 2014]
[edit reason] Please Use example.com [webmasterworld.com] [/edit]

9:46 pm on Jan 27, 2014 (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

You need two rules and you've only got one of them. The rewrite is the behind-the-scenes half; you also need a...

... Naah, what the ###, I'll just paste in the boilerplate. Ignore the parts you already know.

The Redirect-to-Rewrite Two-Step

Problem: Your dynamically generated pages have long, ugly, hard-to-memorize URLs, probably containing query strings. You want them to have short pretty URLs.

The Solution comes in two parts.

Part 1. Redirect
When a user asks for the long ugly URL, redirect to the short pretty URL. Basic pattern:

RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} queryname=([a-z]+)
RewriteRule longcomplicatedURL http://www.example.com/blahblah/%1? [R=301,L]

The %1 is captured from the original query string, and the final ? means that you now get rid of the query string. In real life it will usually be a little more complicated, but that's the basic process.

user asks for

They get bounced over to

Part 2. Rewrite
You get an incoming request for a short pretty URL-- either from a new arrival or from someone who was redirected in Part 1. The server can't tell the difference.

RewriteRule blahblah/([a-z]+)$ /longcomplicatedURL?queryname=$1 [L]

This time around, you're capturing part of the request and changing it into a query string.

user asks for

They may think that's what they're getting-- it's what the browser's address bar says-- but behind the scenes the page content is really coming from

Now you see why Part 1 had to look at THE_REQUEST. It's for insurance. If something happens later on, your long complicated URL might pass through mod_rewrite again. If it does, you need to be sure it doesn't get re-redirected. Otherwise there will be an infinite loop.

Now wait a minute! Does this mean that if someone starts out asking for "longcomplicatedURL", they go through this whole rigamarole and then they end up right back where they started?

Yup. But they don't know it. They only know what the browser's address bar tells them. Even robots-- yes, even google-- can't tell that they're being rewritten.

The Redirect part of the package-- Part 1-- is not technically necessary. The Rewrite-- Part 2-- will function without it. But redirecting everyone to the same URL means that everyone is now on the same page ... and it avoids nasty things like Duplicate Content.

But you're not done yet.

Part 0.
Before you do anything with Part 1 and Part 2, go over your current site carefully. Make sure that your own links point only to the short pretty URL. Requests for the long complicated URL should come only from outside-- from people with outdated bookmarks, or old links from other sites. Your own site will use only the pretty URLs.
10:42 pm on Jan 27, 2014 (gmt 0)

New User

5+ Year Member

joined:June 20, 2007
votes: 0


Your post was exactly what I was needing, I totally forgot to test with the new url... and as redirect was not set, nothing was happening and I tough the rule had some error...

Thanks a lot my friend,
D. Duarte
12:55 am on Jan 28, 2014 (gmt 0)


WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
votes: 8


if you have '/index/pg/' or '/index/' in every url you probably don't need it.
9:57 pm on Jan 28, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
votes: 0

Don't forget to alter the links on the pages of your site to point to the new URLs.

Clicking a link should not result in a redirect. The redirect is in place for previously bookmarked links and stale searchengine listings.