How to hide a subfolder in url rewriting

2019-08-10 16:49发布

问题:

My site is : www.mysite.com/subfolder/login/index.php

I want the URL there to just be www.mysite.com/login/index.php. I tried modifying the .htaccess file in the root folder as follows:

RewriteRule ^login\/index\.php$ /subfolder/login/index.php [L]

--but the problem is that then it can't use or access the CSS file (style.css) from the login folder.

回答1:

# fix js/images/css
RewriteRule ^.+?/((img|css|js)/.+)$ /subfolder/login/$1 [L,NC]

Try adding this as your first rule in htaccess. Or you can use

# fix js/images/css
RewriteRule ^.+?/((img|css|js)/.+)$ http://www.yourdomain.com/subfolder/login/$1 [L,NC]

You can see more examples and ways of rewriting here: https://wiki.apache.org/httpd/Rewrite



回答2:

Add Another rule for css, or use absolute CSS path e.g.:

RewriteRule login\(.*)\.css /subfolder/login/$1.css [L]


回答3:

The title does not seem to match the question at the end of your description, but to answer the latter: If you have a problem with your assets (images, css, external javascript, etc.) after url rewriting, use absolute paths, for example:

/subfolder/login/style.css

instead of something like:

../style.css    /* this will probably not work when rewriting urls */