简体   繁体   中英

.htaccess redirect to subdirectory of directory

I have searched and searched but for some reason can not get this to work how I need it to. I need to write a rule in htaccess that checks to see if the file exists and if it does not look in a subdirectory of the original directory. For example if the request is:

www.mysite.com/css/main.css

if the file main.css is not found I need to redirect to

www.mysite.com/css/src/main.css

I have tried this in my .htaccess in the /css/ directory

RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^css/?([^/].+)?$ /css/src/$1 [L,R=301]

Seams to me that this should be a very easy task but can not seem to get this working. Also in regards to debugging htaccess how to I log what the rewrite rule is looking for? I have this in my VirtualHost file:

LogLevel trace4

but this does not show where the rewrite rule is trying to go... Thanks for the help!

Your regex doesn't seem right. Use this rule:

RewriteEngine ON

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^css/([^/]+)$ /css/src/$1 [L,R=301,NC]

For your second part you need:

LogLevel trace4
RewriteLog "/val/logs/rewrite.log"

I was able to figure this out, the problem I was having was the redirect here is my .htaccess file in case anyone is having a similar problem:

In my css directory I have the following .htaccess:

RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/].+)?$ src/$1 [L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM