【问题标题】:mod_rewrite rule is not working but I have the correct syntaxmod_rewrite 规则不起作用,但我的语法正确
【发布时间】:2020-06-11 00:30:17
【问题描述】:

所以我的 mod_rewrite 规则无法为 URL 指定特定的 php 文件。

Options -Indexes
php_value short_open_tag 1

#Caching
# 1 HOUR
<FilesMatch "\.(ico|pdf|flv)$">
Header set Cache-Control "max-age=31536000, public"
</FilesMatch>

# 1 HOUR
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=31536000, public"
</FilesMatch>

# 1 HOUR
<FilesMatch "\.(xml|txt|css|js)$">
Header set Cache-Control "max-age=31536000, proxy-revalidate"
</FilesMatch>


<IfModule mod_rewrite.c>
#Rewrite rules
RewriteEngine on

RewriteRule ^about\/special-offers\/?$ about/special-offers.php


# remove www. from all domains
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]

# Add trailing slash
# TODO: This breaks POST data when forwarding to a URL without an ending slash
#RewriteCond %{REQUEST_URI}  !\.(php|html?|gif|jpe?g|JPE?G|png|bmp|js|swf|css|ico|xml|txt|woff2|woff)$
#RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [P,R=301]

# hide .svn folders
RewriteRule ^(.*/)*\.svn/ / [NC,L]

# send all images and misc files to the actual location
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule \.(gif|jpe?g|JPE?G|png|bmp|js|swf|ico|xml|txt) index.php [NC,L]

# send folder to the relevant php file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ $1/$2.php
RewriteRule ^([^/]+)\/([^/]+)\/ $1/$2.php

#RewriteRule ^([^/]+)/([^/]+)/$ $1/$2.php
RewriteRule ^([^/]+)/([^/]+)/$ $1/default.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

# otherwise if the file or directory is not found, process through the index
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
</IfModule>

我在这里有许多其他规则,如果需要,我可以发布它们,但由于这是一个特定规则,我不知道为什么其中一个会覆盖它。方向表示赞赏。

我尝试将它放在我的 mod_rewrite 部分的顶部或底部,但似乎都没有任何影响。

我刚刚注意到,当我转到 /about/special-offers/x(其中 x 是任何字符串)时,它会加载正确的页面。

其他

【问题讨论】:

  • 尝试禁用MultiViews,如果您在虚假 URL 和真实现有文件系统内容之间存在部分重叠,这通常是出现问题的罪魁祸首。
  • 嗯,我已经禁用了 MultiViews 但仍然没有运气。
  • 看看htaccess.madewithlove.be 是否可以对正在发生的事情有所了解;如果没有,请向我们展示您的完整重写设置。
  • (“转义”表达式中的斜线应该是不必要的。但至少根据提到的测试工具,它似乎也没有害处。)
  • 使用该工具似乎是我的第一个显式重写作品 (/special-offers.php)。但是我的子目录重写RewriteRule ^([^/]+)\/([^/]+)\/ $1/$2.php 在此之后也可以工作(/a/b/ 到 a/b.php)...但是在该规则之后是另一个重定向到 /about/default.php,最后重定向到 /index 的规则。 php。我禁用了多视图,重写的优先顺序是从上到下还是从下到上?

标签: .htaccess mod-rewrite apache2


【解决方案1】:

终于解决了这个问题,我用 [L] 将 RewriteRule 标记为 final。

相关部分:

# send folder to the relevant php file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)\/([^/]+)\/ $1/$2.php [L]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多