使用.htaccess重定向,不带尾随SLASH

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

我是一个长期的读者,从来没有问过一个问题。大多数只是通过阅读这个非凡的论坛得到答案。最近我一直在试图解决一个问题与htaccess的几天了&它已经阻止了我在我的网络项目atm的进展。您的帮助将不胜感激。
服务器:OpenLiteSpeed
当前规则集.htaccess(.htaccess位于根目录中)

RewriteEngine On
Options +FollowSymlinks -MultiViews -Indexes
DirectorySlash Off

RewriteBase /

#set default characterset from server end
AddDefaultCharset UTF-8

### Rewrite Rules Added by CyberPanel Rewrite Rule Generator
RewriteCond %{HTTP_HOST} ^www.(.*)$
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]

### End CyberPanel Generated Rules.

### Rewrite Rules Added by CyberPanel Rewrite Rule Generator

RewriteCond %{HTTPS}  !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

### End CyberPanel Generated Rules.

#By blocking access from libwww-perl you can eliminate many simpler attacks
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]

## EXPIRES CACHING ##
# Add or remove file types if needed
ExpiresActive On
<FilesMatch "\.(jpg|jpeg|woff|woff2|png|gif|swf|ico|js|css|ttf|eot|ico|mp4|webm|svg|json|webp)$">
Header set Cache-Control "max-age=31536000, public, no-transform"
</FilesMatch>

## EXPIRES CACHING ##

#main landing page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ main.php [L]

问题
1.我有一个名为:/packages/(位于我的服务器的根目录中)。
1.我希望将/packages & /packages/example/example/example模式中的所有查询捕获到一个名为以下文件的文件中:packages_main.php,它也位于根目录中。
1.我已尝试以下规则和更多规则和变体

RewriteRule ^packages/(.+) package_main.php?qsa=$1 [L,QSA]
RewriteRule ^packages$ package_main.php [L,QSA]
RewriteRule ^packages package_main.php [L,QSA]
RewriteRule ^packages(.+) package_main.php [L,QSA]

**问题是,每当我打开https://example.com/packages时,它都会被重定向到https://example.com/packages/**在末尾添加一个尾随斜杠。

我希望它保持像https://example.com/packages,这样我就可以直接将查询字符串传递到页面,例如https://example.com/packages?p=10&a=11,但它重定向到https://example.com/packages/?p=10&a=11,这会混淆我的代码。
我想捕获除查询字符串之外的所有url,这些查询字符串将直接传递给package_main.php,例如https://example.com/packages?p=100https://example.com/packages/abc/bbc?p=100https://example.com/packages/abc?p=100
这花了我3天的时间,但我似乎不能弄清楚它。所以基本上我想要一个规则,将我所有的example.com/packages重定向到package_main. php,而不添加一个尾随斜杠。因为斜杠将查询字符串转换为package_main. php的路径,以便理解&它被错误地拾取。有人能帮助吗?

xpcnnkqh

xpcnnkqh1#

请使用显示示例尝试以下.htaccess规则文件请确保在测试URL之前清除浏览器缓存

RewriteEngine On
Options +FollowSymlinks -MultiViews -Indexes
DirectorySlash Off

RewriteBase /

#set default characterset from server end
AddDefaultCharset UTF-8

### Rewrite Rules Added by CyberPanel Rewrite Rule Generator
RewriteCond %{HTTP_HOST} ^www.(.*)$
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]

### End CyberPanel Generated Rules.

### Rewrite Rules Added by CyberPanel Rewrite Rule Generator

RewriteCond %{HTTPS}  !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

### End CyberPanel Generated Rules.

#By blocking access from libwww-perl you can eliminate many simpler attacks
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]

## EXPIRES CACHING ##
# Add or remove file types if needed
ExpiresActive On
<FilesMatch "\.(jpg|jpeg|woff|woff2|png|gif|swf|ico|js|css|ttf|eot|ico|mp4|webm|svg|json|webp)$">
Header set Cache-Control "max-age=31536000, public, no-transform"
</FilesMatch>

## EXPIRES CACHING ##

RewriteCond %{QUERFY_STRING} ^$
RewriteRule ^packages/?(.*)?$ package_main.php?qsa=$1 [L,QSA]

#main landing page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ main.php [L]

相关问题