【发布时间】:2015-05-21 19:37:14
【问题描述】:
我有一个 .htaccess 文件。它需要在两种情况下重写 URL:对于 MVC 框架和更特殊的情况。如果用户没有名为“cfnw-hash”的 cookie,我想重写对“/resources/newspaper”的任何请求。我尝试将代码放在 MVC 代码之前和之后。不工作。它在切换到 MVC 框架之前工作,虽然我真的没有足够的 .htaccess 知识来查看是否导致问题。代码如下:
Options -Indexes
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteBase /
# Force to exclude the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_COOKIE} !cfnw-hash [NC]
RewriteRule \/resources\/newspaper.* http://www.example.com/error/401 [R=401,NC,L]
【问题讨论】:
标签: regex apache .htaccess mod-rewrite cookies