rewriting URLs for SEO with .htaccess

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I tried asking this in another newsgroup but still couldn't solve the

I have sites with ugly URLs like this:

and after a while I made an .htaccess file to rewrite the URLs to this
search engine friendly format:

But now each page has two URLs that the search engines are spidering -- the
old dynamic one (which still exists) and the new "static" one.  I don't want
the dynamic URLs to exist anymore.

I want to use a 301 redirect from the old dynamic URLs to the new
static-looking ones, but I can't get it to work.  This is my current
.htaccess file:

# This part works:
RewriteEngine On
RewriteRule ^(.+)(\.htm)$ /index.php?p=$1 [L]

# This is the part that doesn't work:
RedirectMatch 301 /index.php?p=(.*)$ $1.htm

I'm wondering:

Is the [L] a problem?
Should the ? in the RedirectMatch be escaped by a backslash?
Do I need an [R] somewhere?
Do I need to escape all my periods with backslashes?

I'm experimenting with these but haven't had any luck yet.  I also added a
forward slash, which was recommended to me, so it looks like this now:
RedirectMatch 301 /index.php?p=(.*)$ /$1.htm

Through a lot of experimentation I've narrowed the problem down to this one

I know this because I've tried regular redirects without success, for
RedirectMatch 301 /index.php?p=(.*)$
Redirect 301 /index.php?p=page1

and it doesn't work.  But this works:
Redirect 301 /

Any advice would be appreciated...

Re: rewriting URLs for SEO with .htaccess

Quoted text here. Click to load it
Quoted text here. Click to load it

RewriteRule ^(.+)\.htm$ /index.php?p=$1 [R=301,L]

(also: removed the second pair of capturing (), they are not needed.)

Quoted text here. Click to load it

I combined this one in the above.

Quoted text here. Click to load it


see above

in the regular expressions -> yes, unless you mean "any character".

Quoted text here. Click to load it


Quoted text here. Click to load it

In the above the problem is the unescaped ? (also, you should escape
each dot in a regex if you mean a real dot, not any character).

John                       Perl SEO tools:
                 Experienced (web) developer:
Get a SEO report of your site for just 100 USD:

Re: rewriting URLs for SEO with .htaccess

Quoted text here. Click to load it

Thanks for your reply.  I tried it but it makes all requests for something
like: redirect to (the opposite of what I want).  I want
the dynamic links to do a 301 redirect to the static ones...

Quoted text here. Click to load it

I tried this also (by itself) and it didn't work:
RedirectMatch 301 /index.php\?p=(.*)$
Also tried:
RedirectMatch 301 /index\.php\?p=(.*)$ (but that
shouldn't make a difference because the unescaped "." can act as a real ".")

I'm clearning the cache every time I change the .htaccess file to make sure
I'm getting a new version of the page.

Any ideas?

Site Timeline