2

I'm trying to use mod rewrite to redirect a subdomain to a file with a parameter in a folder.

The website is in a folder /new in the root.

My .htaccess right now is (placed in the root)

RewriteEngine on
RewriteCond %{HTTP_HOST}   ^[www\.]*new.mydomain.com [NC]
RewriteCond %{REQUEST_URI} !^/new/.*
RewriteRule ^(.*) /new/index.php?cust_id=1807

This is working, it redirects the browser to the right file with the parameter but it breaks all css, js and image links. I know using absolute paths or setting a basepath can solve this easily but I would like to keep using relative paths and get this to work.

e.g. The source now links to css as follows: http://new.mydomain.com/css/file.css while it should be http://new.mydomain.com/new/css/file.css

Thanks in advance!

Edit: Excluding css and image files won't solve this as they are located in e.g. /new/css

2 Answers2

0

EDIT: Based on your feedback

RewriteEngine on
# Redirect css,js,img to /new/(css,js,img) with a 301
RewriteCond %{HTTP_HOST}   ^[www\.]*new.mydomain.com [NC]
RewriteCond %{REQUEST_URI} ^/(css|js|img)/.*
RewriteRule ^(.*)$ /new/$1 [L,R=301]

# then redirect anything that's not already /new/ to the page you want
RewriteCond %{HTTP_HOST}   ^[www\.]*new.mydomain.com [NC]
RewriteCond %{REQUEST_URI} !^/new/.*
RewriteRule ^(.*) /new/index.php?cust_id=1807

This is based off the limited info I have, see my comment below.

Wing Lian
  • 2,387
  • 16
  • 14
  • thanks for this, but the same problem applies as with the solution 'He Was' gave earlier –  Dec 10 '12 at 08:18
  • Your rewrite rule basically says to redirect EVERYTHING to /new/index.php?cust_id=1807, which I doubt is the intended behavior. Are you redirecting every page to /new/index.php?cust_id=1807 or should every page be mapped to /new/ ? If you can provide that, I have a better solution in mind. – Wing Lian Dec 10 '12 at 19:42
  • Every page should be remapped to that link. Like I mentioned, it's for a subdomain. –  Dec 21 '12 at 13:45
0

How about adding another rewriteCond? Something like...

RewriteCond %{REQUEST_URI} !^.*\.(css|js|jpe?g|png|gif)$

This will exclude any files with those suffixes from the rewrite.

By the way, I don't think that this [www\.]* looks correct in your first RewriteCond ... it means 'zero or more occurences of w or w or w or .' so it's not really doing anything useful.

foundry
  • 31,615
  • 9
  • 90
  • 125
  • I tried that already. The problem is that css files and images are in a subfolder within that /new folder. After redirection it's looking for those folder from the root up. I think I need another rule to forward requests to those files to the /new folder. e.g /css/file.css to /new/css/file.css –  Dec 10 '12 at 08:17