.htaccess在输入错误的URL时不会激活404规则

vyswwuz2  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(125)

我有一些规则可以很好地访问类别/子类别/产品
但如果我输入一个不存在的URL,而不是激活404规则,访问principal.php
也适用于图像和其他扩展如何取消除以下呼叫之外的所有呼叫的规则:

  • mydomain.com/MyCategory
  • mydomain.com/MyCategory/MySubcategoy
  • mydomain.com/MyCategory/MySubcategoy/MyProduct

该规则不应应用于具有扩展名的URL(例如 .html、.css、.php、.pdf等),并且这些扩展名不应出现
这些例子
mydomain.com/contact.php
应该是
mydomain.com/contact
这是我的.htaccess

ErrorDocument 404 file-not-found.html
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^.]*\.html)/?$  $1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^.]*\.php)/?$  $1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ principal.php?cat=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ principal.php?cat=$1&sub=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ principal.php?cat=$1&sub=$2&prod=$3  [L]
w9apscun

w9apscun1#

使用显示示例和尝试,请尝试以下.htaccess规则文件请确保在测试URL之前清除浏览器缓存
由于您不希望有几种格式(例如:html,php等),那么我们可以从您当前的规则集中删除1个规则集,并保留一个,这样总共将是4个规则集。

ErrorDocument 404 file-not-found.html
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(?:html|css|pdf|php)/?$
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$  $1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(?:html|css|pdf|php)/?$
RewriteRule ^([^/]+)$ principal.php?cat=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(?:html|css|pdf|php)/?$
RewriteRule ^([^/]+)/([^/]+)$ principal.php?cat=$1&sub=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(?:html|css|pdf|php)/?$
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ principal.php?cat=$1&sub=$2&prod=$3  [L]

相关问题