Right now I have the following code to redirect all non-www URLs to www URLs:

RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

Besides that redirection, I also want to redirect all calls to /dev/ to dev.example.com. I've tried a bunch of different approaches but nothing has worked (the few that get close like ReplaceMatch will only redirect to the dev.example.com bit and will leave the rest as a query string).

Any ideas?

EDIT: Here's the full .htaccess for the root folder (a few rewritten for privacy reasons):

ErrorDocument 404 /404.html
AddType application/x-httpd-php .json
AddType application/x-httpd-php .xml

Options +FollowSymlinks
RewriteEngine On
RewriteBase /

# disables image hotlinking
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?example\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !google\. [NC] 
RewriteCond %{HTTP_REFERER} !search\?q=cache [NC]
RewriteRule .*\.(jpe?g|gif|bmp|png)$ - [F]

# Stop htaccess rules from accessing these areas of the website.
RewriteRule ^(banner|boxart|cgi-bin|content|forums|includes|newsimages|newsimg|sidebar|union-files|google5e645385256c3a63.html) - [L]

# Redirects /page URLs to /page/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) /$1/ [R=301,L]

# Redirect old URLs to their new URLs
RewriteRule ^reviews/badurl--43.html$ http://www.gamingunion.net/reviews/goodurl--43.html [R=301,NC]

# Redirect non-WWW to WWW
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RedirectMatch 301 ^/dev/(.*)$ http://dev.example.com/$1

# URL Directory Redirects
RewriteRule ^([^/]*)/$ /index.php?p=$1&urltype=slash [L,QSA]
RewriteRule ^([^/]*)/$ /index.php?p=$1&urltype=slash [L,QSA]
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?p=$1&lv1d=$2 [L,QSA]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/$ /index.php?p=$1&lv1d=$2&lv2d=$3 [L,QSA]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/$ /index.php?p=$1&lv1d=$2&lv2d=$3&lv3d=$4 [L,QSA]

# URL File Redirects
RewriteRule ^([^/]*)\.html$ /index.php?p=$1&urltype=dot [L,QSA]
RewriteRule ^([^/]*)/([^/]*)\.html$ /index.php?p=$1&lv1p=$2 [L,QSA]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?p=$1&lv1d=$2&lv2p=$3 [L,QSA]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?p=$1&lv1d=$2&lv2d=$3&lv3p=$4 [L,QSA]

# For servers that support output compression, you should pick up a bit of speed but un-commenting the following lines.
php_flag zlib.output_compression On
php_value zlib.output_compression_level 5

The one in /dev/ is basically the same as this one, except that it has a password protection bit at the top and has a few redirects for some image folders that only reside on the root of the server.

3 Answers 3


So .. you have more than 1 .htaccess file: one in the root folder and one in /dev folder.

Where did you put rules from @Alex -- root or /dev .htaccess? Most likely in root. That is the reason why it did not worked.

Thing is -- if Apache sees rewrite rules in lower level folder, it will NOT execute them from parent folder UNLESS your lower level .htaccess has RewriteOptions inherit line: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriteoptions

Now, you have added the below code into the .htaccess:

RedirectMatch 301 ^/dev/(.*)$ http://dev.example.com/$1

This directive belongs to another module and gets executed anyway .. and it executes after mod_rewrite have already rewritten the URL. That's why you see real URLs after redirect.

The rule below should work -- just put it into your /dev/.htaccess

RewriteCond %{HTTP_HOST} !^dev\.example\.com
RewriteCond %{REQUEST_URI} ^/dev/
RewriteRule ^(.*)$ http://dev.example.com/$1 [R=301,L,QSA]

It tells Apache to redirect if:

  • domain is not dev.example.com
  • URL starts with /dev/ (which will only happened if requested via main domain)
  • When I use that code I should have removed the RedirectMatch from the root, right? If so, adding the code you provided in the dev subfolder didn't do anything. Commented Jul 5, 2011 at 20:38
  • @Shawn I have updated my answer with new rules (I edited wrong file when tested these rules myself -- I edited main htaccess instead the one from sub folder). The changes are minimal.
    – LazyOne
    Commented Jul 5, 2011 at 20:53

Try this:

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^dev/(.*) http://dev.example.com/$1 [R=301,L]
  • Doesn't appear to do anything. Commented Jul 5, 2011 at 6:58
  • And if you comment the rules that redirect non-www to www?
    – Alex
    Commented Jul 5, 2011 at 7:04
  • If not, since you have the non-www to www redirect, try this one: RedirectMatch 301 ^/dev/(.*)$ http://dev.example.com/$1
    – Alex
    Commented Jul 5, 2011 at 7:07
  • That one works, but it causes a URL like http://dev.example.com/ps3/?p=ps3&urltype=slash. Is there something that can be modified in the RedirectMatch or is there some other area of the htaccess file that's affecting things? (I'll be heading to bed in a few minutes, so I won't be able to look at this again till tomorrow morning.) Commented Jul 5, 2011 at 7:14
  • 2
    @Shawn Post your whole .htaccess contents here (update your question) -- looks like you have rules in wrong order (RedirectMatch executes after RewriteRules that's why you see real URLs instead of nice one).
    – LazyOne
    Commented Jul 5, 2011 at 9:00

I've used the following redirect-code to rewrite any sub-domains to a URL-parameter:

# rewrite subdomain
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.org$ [NC]
RewriteRule ^(.*)$ index.php?q=116&company_name=%1$1 [L,QSA,NC]

%1 is the regex-match for the subdomain; $1 consists of all other URL-params;

Play around with it, to redirect to a folder, like:

RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.org$ [NC]
RewriteRule ^(.*)$ /%1/$1 [L,QSA,NC]
  • Maybe it's how my current configuration is, but I got infinite redirect loops whenever I tried using that. Commented Jul 5, 2011 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.