mod rewrite subdomain to folder break paths to css and images

扶醉桌前 提交于 2019-12-13 16:25:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!