Forum Moderators: phranque

Message Too Old, No Replies

Rewriting a url with a question mark

         

glimbeek

9:28 am on Feb 5, 2010 (gmt 0)

10+ Year Member



Say for instance I have the following URL:

http://www.example.com/news/what-has-happend-now?/

How do I rewrite that to:

http://www.example.com/news/what-has-happend-now/

Thanks in advance.

g1smd

9:30 am on Feb 5, 2010 (gmt 0)

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



Do you mean rewrite, or redirect?

glimbeek

11:01 am on Feb 5, 2010 (gmt 0)

10+ Year Member



Uhmm... a rewrite

RewriteRule ^what-has-happend-now?/$ http://www.example.com/news/what-has-happend-now/ [R=301,L]

Like that, sort off.. because that doesn't work ;)

g1smd

11:39 am on Feb 5, 2010 (gmt 0)

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



That isn't a rewrite. That code is for a redirect.

Terminology is key here, as is the EXACT syntax for coding these rules.

As coded, your rule will accept any of these requests:

example.com/what-has-happend-no/
example.com/what-has-happend-now/
www.example.com/what-has-happend-no/
www.example.com/what-has-happend-now/

with, or without, any appended query string and will redirect to the new URL, re-appending any query string (if present).

The question mark is used to show characters that are optional. If you need a literal question mark, then you need to escape it.

glimbeek

1:18 pm on Feb 5, 2010 (gmt 0)

10+ Year Member



"The question mark is used to show characters that are optional."

I know, but the question mark is in the URL and I don't want it to be in the URL. Hence me putting it their in my example. The URL is made up from the article title and in the title there's a question mark.

So I use RewriteRule ^what-has-happend-now\?/$ http://www.example.com/news/what-has-happend-now/ [R=301,L]

?
I tried that, and that doesn't work. I end up with:
http://www.example.com/news/what-has-happend-now/?/

jdMorgan

4:07 pm on Feb 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteCond %{THE_REQUEST} ^[A-Z]+\ /what-has-happend-now\?/\ HTTP/
RewriteRule ^what-has-happend-now$ http://www.example.com/news/what-has-happend-now/? [R=301,L]

Note that the value of "THE_REQUEST" is the client's HTTP request line, exactly as it appears in your raw server access logs. Example:
GET /what-has-happend-now?/ HTTP/1.1


Note also that the question mark at the end of the RewriteRule's substitution URL is NOT a literal question mark. It is a mod_rewrite operator that clears the current query string, and is required.

Because you're dealing with the way that query strings are appended to URLs and the way that Apache handles them, this code looks all wrong. But it's very likely correct.

Finally, note that the word "happened" is miss-spelled consistently as "happend" throughout this thread. We're all assuming that this was intentional.

Jim

glimbeek

8:08 am on Feb 9, 2010 (gmt 0)

10+ Year Member



The miss spelling of happened is due to my crappy English ;)

Thanks for the explanation jdMorgan, I'll give it a go.