Regex Trouble

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
vjeleven
Forum Newbie
Posts: 3
Joined: Thu Jun 26, 2008 4:54 am

Regex Trouble

Post 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.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Regex Trouble

Post by prometheuzz »

Try this:

Code: Select all

RewriteRule   ^.*/[?]n=([^/]+)/?$   names/$1
(untested!)
vjeleven
Forum Newbie
Posts: 3
Joined: Thu Jun 26, 2008 4:54 am

Re: Regex Trouble

Post 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.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Regex Trouble

Post 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.
vjeleven
Forum Newbie
Posts: 3
Joined: Thu Jun 26, 2008 4:54 am

Re: Regex Trouble

Post 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.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Regex Trouble

Post 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!
Post Reply