问题
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
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/13790313/mod-rewrite-subdomain-to-folder-break-paths-to-css-and-images