Page 1 of 1

Regex Trouble

Posted: Fri Aug 22, 2008 6:34 am
by vjeleven
Hi,

I think this is a rather simple thing to solve, but I've been unable to do it so far !!

I have several redirect rules set up in my .htaccess (most of them involving regex), but I am unable to get the following one working:

1. I need to be able to redirect all requests coming to ".../?n=X" to "http://mysite.com/names/X" - I am unable to match the "\?n=*" part using regex!

Can somebody please help me.

Any help will be of great value to me. Thanks in advance.

Re: Regex Trouble

Posted: Fri Aug 22, 2008 6:43 am
by prometheuzz
Try this:

Code: Select all

RewriteRule   ^.*/[?]n=([^/]+)/?$   names/$1
(untested!)

Re: Regex Trouble

Posted: Fri Aug 22, 2008 7:06 am
by vjeleven
Hi prometheuzz,

Thank you very much for the help.

However, the solution you suggested is not working, as it is somehow continuing to not match the "?n=X" part (where X is the only thing that can change, for ex: X can be 1, 34, 4567 etc..)

Thank You.

Re: Regex Trouble

Posted: Fri Aug 22, 2008 7:09 am
by prometheuzz
vjeleven wrote:Hi prometheuzz,

Thank you very much for the help.

However, the solution you suggested is not working, as it is somehow continuing to not match the "?n=X" part (where X is the only thing that can change, for ex: X can be 1, 34, 4567 etc..)

Thank You.
I'm not sure what you mean by "continuing to not match the "?n=X" part". Could you tell me what IS and what ISN'T matched when you use my suggestion?

Note that I don't have access to an Apache server with that module installed, so my help is rather limited.

Good luck.

Re: Regex Trouble

Posted: Fri Aug 22, 2008 7:19 am
by vjeleven
Hi prometheuzz,

For ex:

if i am giving a simple redirect rule like:
RewriteRule n=1234 http://mysite.com
it is correctly when redirecting when we try a URL thats like .../n=1234

Now, when I try to The moment I put in the '?' mark before it, it does not match

As you suggested, i tried:
RewriteRule ^.*/[?]n=([^/]+)/?$ names/$1

But it does not work and gives me a 404 !! And the 404 is not because names/X does not exist. It does exist.

Thank You.

Re: Regex Trouble

Posted: Fri Aug 22, 2008 7:22 am
by prometheuzz
vjeleven wrote:Hi prometheuzz,

For ex:

if i am giving a simple redirect rule like:
RewriteRule n=1234 http://mysite.com
it is correctly when redirecting when we try a URL thats like .../n=1234

Now, when I try to The moment I put in the '?' mark before it, it does not match

As you suggested, i tried:
RewriteRule ^.*/[?]n=([^/]+)/?$ names/$1

But it does not work and gives me a 404 !! And the 404 is not because names/X does not exist. It does exist.

Thank You.
What happens when you provide the entire url:

Code: Select all

RewriteRule ^.*/[?]n=([^/]+)/?$ http://mysite.com/names/$1
If that also doesn't work, I'm afraid I can't help you: you'll have to wait for someone more familiar with mod_rewrite.

Good luck!