使用 htaccess 仅删除 URL 中的 2 个 public 值之一

Removing only one of the 2 public values in the URL with htaccess

我有一个用Laravel 8编写的电子商务系统。 Public 目录出现在 URL.

本软件的URL系统与网络中一样

主页:website.com/public/类别:website.com/public/category 产品:website.com/public/category

CSS文件URL地址:website.com/**public**/themes/themename/**public**/css/style.css JS文件URL地址:website.com/**public**/themes/themename/**public**/js/app.js

可以看到,CSS和JS文件中有2个public个值

我按如下方式更新了 htaccess 文件以删除 public 值。

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.website\.com
RewriteRule (.*) https://www.website.com/ [R=301,L]

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#remove public/ from URLs using a redirect rule
RewriteCond %{THE_REQUEST} \s/+(.+/)?public/(\S*) [NC]

#RewriteRule ^ /%1%2? [R=301,L,NE]
#Remove index.php
#RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
#RewriteRule (.*?)index\.php/*(.*) / [R=301,NE,L]

#Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

#Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

#Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

但是,它也删除了 CSS 和 JS 文件的第 2 个 public 值。这就是 CSS 和 JS 文件失败的原因。

如何保持第二个 public 值不变并删除第一个 public 值?

按照以下方式获取您的 htaccess 文件,请确保在测试您的 URL 之前清除浏览器缓存。我也更正了您其他规则中的一些标志。

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews -Indexes
</IfModule>

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.website.com%{REQUEST_URI} [NE,R=301,L]

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

# remove public/ from URLs using a redirect rule
RewriteCond %{THE_REQUEST} \s/(themes/themename)/public(/css/app\.css|/js/app\.js)\s  [NC]
RewriteRule ^ %1%2? [R=301,L]
RewriteRule ^(themes/themename)/(css/app\.css|/js/app\.js)/?$ /public/ [NC,L]


# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]