We help IT Professionals succeed at work.

URL Rewrite Help

Hi,

I need assistance in creating a URL rewrite directive to rewrite a url like this:

http://tmaterialsac.com/?aff=sgreene 

to this:

http://tmaterialsac.com/aff.html?p=sgreene&w=tmac

where the sgreene is variable and may contain any number of letters or digits

This is using apache mod_rewrite on a linux server.
Comment
Watch Question

Something like this should work...

RewriteEngine On
RewriteCond %{QUERY_STRING} ^aff=(.*)$
RewriteRule ^/ /aff.html?p=%1&w=tmac

Open in new window

Author

Commented:
That doesn't seem to be working. I tried it - no luck, then added a [R,NC] to the end of the rule - hoping that would actually show the redirected URL in the address bar - still nothing of course...

are you sure about the % sign in the rewrite rule? I thought it should be a $ - tried that anyway - still no luck
Top Expert 2011

Commented:
Hmm, try the following
RewriteEngine On
RewriteCond %{QUERY_STRING} ^aff=(.*)$
RewriteRule ^/?aff=$1 /aff.html?p=$1&w=tmac

Open in new window


Top Expert 2011

Commented:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^aff=(.*)$ [NC]
RewriteRule ^/?aff=$1 /aff.html?p=$1&w=tmac [R=301,NC,L]

Open in new window

Top Expert 2011

Commented:
I think the %1 back reference can only be used in a RewriteCond.
Top Expert 2011

Commented:
I'm not sure if the match I put in the for RewriteRule is correct on second thought... I was focused on the %1 issue.  Sorry for the spam, and apologies that I can't test it right now.
Top Expert 2011
Commented:
I need to stop answering questions from my friends iPad :-/  pain in the butt anyways.

Now that I can test, what Jeremy wrote works in regards to the %1 in a RewriteRule, and adding the Redirect flag to that rule will produce the results you require.

I'll just walk away now and pretend this never happened :p

Good luck.

[root@broken conf.d]# wget http://zippo.domain.co/?aff=blah
--2011-11-03 20:41:01--  http://zippo.domain.co/?aff=blah
Resolving zippo.domain.co... 206.125.x.x
Connecting to zippo.domain.co|206.125.x.x|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://zippo.domain.co/aff.html?p=blah&w=tmac [following]
--2011-11-03 20:35:48--  http://zippo.domain.co/aff.html?p=blah&w=tmac

Open in new window

RewriteEngine  on
RewriteCond %{QUERY_STRING} ^aff=(.*)$ [NC]
RewriteRule ^/ /aff.html?p=%1&w=tmac [R=301,NC,L]

Open in new window


Author

Commented:
This still isn't working for me - is there something else that I might be missing? Something with the hosting setup or ???

This looks to me like it should work, but no luck.
Top Expert 2011

Commented:
There are a few things that could be.  What are your logs showing? Are you sure mod_rewrite is loaded?

If it is, try adding the following along with your rewrite rule.
Options +FollowSymLinks

Open in new window

If that doesn't work, then it could be an issue with your AllowOverride directives and/or Options.
Yes, you need the "%<no.>" instead of "$<no.>"...

$ - for strings caught in uri, % - for strings caught in query string.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.