.htaccess文件与其他规则冲突

kq4fsx7k  于 2023-03-23  发布在  其他
关注(0)|答案(2)|浏览(111)
Options -MultiViews

RewriteEngine on

Header set Strict-Transport-Security "max-age=31536000" env=HTTPS

Redirect /index https://example.com/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R] # <- for test, for prod use [L,R=301]

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

RewriteCond %{HTTP_HOST} https://example.com/$ [NC] 
RewriteRule (.*) https://example.com/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

# Corporate Gifts
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^corporate-gifts/([^/]+)$ gift-category.php?slug=$1 [L]

# Services
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^services/([^/\.]+)/?$ category.php?slug=$1 [L]
RewriteRule ^services/([^/\.]+)/([^/\.]+)$ sub-category.php?slug1=$1&slug=$2 [L]

#Uncomment the following lines as you create the pages.
#ErrorDocument 400 /400.php 
#Bad Request 

#ErrorDocument 401 /401.php
#Unautorized

#ErrorDocument 403 /403.php
#Forbidden

ErrorDocument 404 https://example.com/404.php
#Not Found

#ErrorDocument 500 /500.php
#Internal Server Error

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php80” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php80___lsphp .php .php8 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

在我们的网站有两种类型的类别一个是服务和第二个是企业礼品.对于服务类别页面我添加重写规则如上.htaccess代码.我们的服务页面菜单是这样的(服务-〉制服-〉校服)动态值来自管理端。对于第二个选项卡菜单企业礼品我们创建一个。php页面名称是企业-gifts.php。我需要这样的企业礼品页面菜单(企业礼品-〉服装-〉徽章).当我们点击页面企业礼品其工作正常,但当我点击服装页面的子类别,然后企业礼品主类别页面打开,而不是然后子类别.如何应用.htaccess规则在菜单栏中的不同类别.
我尝试上面。htaccess代码,我需要一个解决冲突的问题。

hvvq6cgz

hvvq6cgz1#

您可以通过使规则更具体并确保它们不重叠来解决此问题:

RewriteEngine On

# Corporate Gifts
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^corporate-gifts/([^/]+)$ gift-category.php?slug=$1 [L]

# Services
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^services/([^/\.]+)/?$ category.php?slug=$1 [L]
RewriteRule ^services/([^/\.]+)/([^/\.]+)$ sub-category.php?slug1=$1&slug=$2 [L]

Test

41ik7eoe

41ik7eoe2#

Options -MultiViews

RewriteEngine on

Header set Strict-Transport-Security "max-age=31536000" env=HTTPS

Redirect /index https://example.com/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R] # <- for test, for prod use [L,R=301]

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

RewriteCond %{HTTP_HOST} https://example.com/$ [NC] 
RewriteRule (.*) https://example.com/$1 [R=301,L]

# Corporate Gifts
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^corporate-gifts/([^/]+)$ gift-category.php?slug=$1 [QSA,L]

# Services
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^services/([^/\.]+)/?$ category.php?slug=$1 [L]
RewriteRule ^services/([^/\.]+)/([^/\.]+)$ sub-category.php?slug1=$1&slug=$2 [L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

#Uncomment the following lines as you create the pages.
#ErrorDocument 400 /400.php 
#Bad Request 

#ErrorDocument 401 /401.php
#Unautorized

#ErrorDocument 403 /403.php
#Forbidden

ErrorDocument 404 https://example.com/404.php
#Not Found

#ErrorDocument 500 /500.php
#Internal Server Error

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php80” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php80___lsphp .php .php8 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

我张贴我的正确方法的答案。我改变了我的代码规则的位置,现在它的工作没有冲突的问题罚款。

相关问题