Mobile app version of vmapp.org
Login or Join
Angie530

: Subfolder won't permenantly redirect to subdomain 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/

@Angie530

Posted in: #301Redirect #Htaccess #Redirects

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

RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ www.example.com/ [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 (.*) // [R=301,L]

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

# Redirect non-WWW to WWW
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ www.example.com/ [R=301,L]
RedirectMatch 301 ^/dev/(.*)$ dev.example.com/
# URL Directory Redirects
RewriteRule ^([^/]*)/$ /index.php?p=&urltype=slash [L,QSA]
RewriteRule ^([^/]*)/$ /index.php?p=&urltype=slash [L,QSA]
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?p=&lv1d= [L,QSA]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/$ /index.php?p=&lv1d=&lv2d= [L,QSA]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/$ /index.php?p=&lv1d=&lv2d=&lv3d= [L,QSA]

# URL File Redirects
RewriteRule ^([^/]*).html$ /index.php?p=&urltype=dot [L,QSA]
RewriteRule ^([^/]*)/([^/]*).html$ /index.php?p=&lv1p= [L,QSA]
RewriteRule ^([^/]*)/([^/]*)/([^/]*).html$ /index.php?p=&lv1d=&lv2p= [L,QSA]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*).html$ /index.php?p=&lv1d=&lv2d=&lv3p= [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.

10.03% popularity Vote Up Vote Down


Login to follow query

More posts by @Angie530

3 Comments

Sorted by latest first Latest Oldest Best

 

@Sims2060225

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: httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriteoptions
Now, you have added the below code into the .htaccess:

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

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 ^(.*)$ dev.example.com/ [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)

10% popularity Vote Up Vote Down


 

@Nimeshi995

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

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


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

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

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^([^.]+).example.org$ [NC]
RewriteRule ^(.*)$ /%1/ [L,QSA,NC]

10% popularity Vote Up Vote Down


 

@Lee4591628

Try this:

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

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme