regex - Htaccess 重写显示 404 未找到

标签 regex apache .htaccess mod-rewrite

我的 htaccess 文件中有这条重写规则,删除了 URL 上的 index.php 以仅显示域名

RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]

但它似乎显示 404 not found on all other directories。例如,我有一个名为 admin 的目录,如果我转到 www.domain.com/admin,我会收到 404 not found

我在 RewriteCond 代码上面也有这段代码

RewriteCond %{REQUEST_URI} !^/admin [NC]

这是我的完整 htaccess 文件

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/admin [NC]
RewriteCond %{REQUEST_URI} !^/status [NC]
RewriteCond %{REQUEST_URI} !^/customer [NC]

RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]

# Rewrites /services to be /home.php?id=services
RewriteRule ^([a-zA-Z0-9-]+)/?$ /home.php?id=$1 [L,QSA]

#BLOG
# Rewrites /blog/this-is-a-blog-post to be /home.php?id=blog&slug=this-is-a-blog-post
RewriteRule ^([a-zA-Z0-9-]+)/([\w-]+)/?$ /home.php?id=$1&slug=$2 [L,QSA]

# Rewrites /blog/year2013/month12 to be /home.php?id=blog&year=2013&month=01
RewriteRule ^([a-zA-Z0-9-]+)/year([0-9]+)/month([0-9]+)/?$ /home.php?id=$1&year=$2&month=$3 [L,QSA]

最佳答案

我相信服务规则在这里引起了问题。将其更改为:

# Rewrites /services to be /home.php?id=services
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-]+)/?$ /home.php?id=$1 [L,QSA]

对博客规则也做同样的事情:

#BLOG
# Rewrites /blog/this-is-a-blog-post to be /home.php?id=blog&slug=this-is-a-blog-post
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-]+)/([\w-]+)/?$ /home.php?id=$1&slug=$2 [L,QSA]

关于regex - Htaccess 重写显示 404 未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20729553/

相关文章:

Python正则表达式非贪婪表现得像贪婪

php - 从某个页面中删除 https

html - 针对移动设备和 CSS @font-face 进行优化

.htaccess - 允许使用 htaccess 访问一个目录吗?

javascript - 零次或多次匹配对于零次匹配无法正常工作

javascript - 如何使用正则表达式的现有结果再次过滤

php - MySQL REGEXP 匹配邮政编码

php - Apache - 浏览任何域 URL 都指向同一文件夹

apache - 使用 .htaccess 规则递归重定向所有 PDF?

apache - Mod_rewrite 正在将/var/www 添加到生成的 URL