Forum Moderators: phranque

Message Too Old, No Replies

Rewrite Rule Problem

Apache rewrite ruule

         

shahidpazeez

9:53 am on Jun 16, 2008 (gmt 0)

10+ Year Member



Hai all

I am writing a rewrite rule for redirecting the sub domain

Example : abc.example.com to www.example.com/user/abc

My .htaccess file is

RewriteCond %{HTTP_HOST} ^(([a-zA-Z0-9_-]+)+\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.example.com%{REQUEST_URI}/user/%1 [R=301,L]

The problem is that when I use this rule the URL :

abc.example.com will be redirected to example.com/user/abc.

At the end a dot(.) is comming. is any solution for this...

SteveWh

10:38 am on Jun 16, 2008 (gmt 0)

10+ Year Member



I'm not an expert at this, but in your group (which becomes %1), the last thing in it is the dot: \.
It's being grabbed and then appended along with abc. Move the trailing dot outside the group.

^(([a-zA-Z0-9_-]+)+\.)?

jdMorgan

3:37 pm on Jun 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem with the original code is that the back-reference should be to %2.

Also, since you're using [NC], there's no need to include both A-Z and a-z in the pattern. Remove one or the other to improve performance. And do not end-anchor the hostname, or it will fail if a FQDN is requested or port number is appended. There also appears to be an extra "+" in there.


RewriteCond %{HTTP_HOST} ^(([a-z0-9_-]+)\.)?example\.com [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.example.com%{REQUEST_URI}/user[b]/%2[/b] [R=301,L]

Count left parentheses to determine the back-reference number.

Jim