Forum Moderators: phranque

Message Too Old, No Replies

htaccess rewrite add extension and preseve query string

help with htaccess adding extension with query string

         

danoneil

7:34 pm on Jun 16, 2011 (gmt 0)

10+ Year Member



I've been trying for ages to work out how to redirect a page on my site...

I need to go from:

example.com/this-page?a=1234567890

to:

example.com/this-page.html?a=1234567890

I've tried various things, the closest I get to it is that it strips out the query string, but redirects to the html extension... otherwise I end up with too many redirects errors!

All good fun, any help is much appreciated.

Thanks, Dan

lucy24

10:58 pm on Jun 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



:: omitting lecture on difference between redirect and rewrite because someone else can do that part better ::

Now then. Has the actual name of the page changed, or is there a problem with incorrect links coming in from other places? If the incorrect links are on your own site you will of course fix them-- but you might still need the temporary rewrites just to point search engines in the right direction.

You need to make sure your rewrite is only picking up those addresses that don't already have .html in them, otherwise you risk ending up with

example.com/this-page.html.html.html.html.html.html?a=1234567890

on into eternity. So you have to make sure the Regular Expression only picks up what it's supposed to:

([\w\-]+)(\?a=\d+)
goes to
$1.html$2 [L]

The first part would work for the name "this-page" because it includes the hyphen. If there is a possibility of subdirectories, this part will get a little fancier. You might get away with [\w\-/], but it is not safe to use an all-purpose .+

For the second part I assumed that it's always "a", and always a string of numbers, as in your example, but that part is easy to fine-tune.

If the original form
example.com/this-page?a=1234567890

has any prior existence-- even if it's only from bad links-- you will need to say [R=301,L]. This means that from now on until forever, the version with .html is the new address of the page. Humans probably don't care-- addresses in this form don't usually get bookmarked-- but search engines might.

g1smd

11:32 pm on Jun 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Which URL does the pages of your site link to? Those links "define" the URL. It is too late to "change" that URL after the link is clicked.

What is the "other" URL? It it an old URL for the content, or is it the real script path for the content pointing to where it actually resides on the server?

If you are using "new" URLs for your content, why didn't you take the opportunity to invoke a URL rewrite and utilise URLs like example.com/this/1234567890 or somesuch?

danoneil

5:49 am on Jun 17, 2011 (gmt 0)

10+ Year Member



Thanks for your inputs...

I'll clarify - I realise that my question was a little vague...

There's a script on my wordpress that adds html extensions to pages - for some reason it won't allow me to remove the extension for one particular page which is dynamically generated by a membership script.

The url is as follows:

my-domain/wishlist-member?reg=nnnnnnnnnn

Which doesn't work because the other script adds the html extension to it. What I want to be able to do (for the sake of all the links that point into that page) is be able to add the .html extension using a rewrite/redirect.

As lucy points out, the main challenge is the eternity of html extensions...

I think it might be something to do with not having escaped the ? using \? instead. I'll take another look at it later today - if you have any input it would be greatly appreciated.

Thanks, Dan