开发者

Redirecting a pattern of old URLs in Apache2 and Drupal

mod_rewrite has always been one of my weakest areas. I've tried to find a solution to this for a while, and many answers simply link to the mod_rewrite documentation, which isn't helpful to me. I believe my problem is pretty simple, so I'm hoping someone won't mind giving me a freebie this time.

I have some existing mod_rewrite rules for Drupal:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

Which, of course, work fine; they're stock Drupal rules.

I want to redirect requests for

  /snippet/yyyy-MMM-dd/name-of-article

to

  /snippets/name-of-article

so I changed the rules to this:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^snippet/20[0-9]{2}-[a-z]{3}-[0-9]{2}/(.*)$ snippets/$1 [L,R=301] ### MY NEW RULE
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

My new rule works as expected, and performs the redirect properly. However, it seems to affect CSS and JS requests, evident by the lack of styling and scriptin,g and 404 errors in the error log. If I try to view a CSS or JS file directly, I get the same HTML output as it I'd requested the parent document. So, obviously, my rule is broken.

I don't know 开发者_运维百科why this is so hard -- seems simple.


Okay, well, I learned something. RewriteCond affect the RewriteRule they precede. Duh!

This works:

RewriteEngine on
RewriteBase /

RewriteRule ^snippet/20[0-9]{2}-[a-z]{3}-[0-9]{2}/(.*)$ snippets/$1 [L,R=301] ### MY NEW RULE

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜