【问题标题】:Slug getting cut laravel蛞蝓被割伤 laravel
【发布时间】:2022-01-25 09:58:35
【问题描述】:

目前我有我的 laravel 项目的暂存链接

http://**.***.**.***/project-name/cart

如果项目像这样访问http://**.***.**.***/project-name/cart

它是可访问的,但如果你像这样访问它

http://**.***.**.***/project-name/cart/

当购物车后面有斜线时,它会重定向到http://**.***.**.***/cart

导致找不到

这和Htaccess有关吗?

顺便说一句,这是我的 htaccess 代码

 #<IfModule mod_deflate.c>
 #  # Compress HTML, CSS, JavaScript, Text, XML and fonts
 #  AddOutputFilterByType DEFLATE application/javascript
 #  AddOutputFilterByType DEFLATE application/rss+xml
 #  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
#  AddOutputFilterByType DEFLATE application/x-font
 #  AddOutputFilterByType DEFLATE application/x-font-opentype
 #  AddOutputFilterByType DEFLATE application/x-font-otf
 #  AddOutputFilterByType DEFLATE application/x-font-truetype
#  AddOutputFilterByType DEFLATE application/x-font-ttf
 #  AddOutputFilterByType DEFLATE application/x-javascript
 #  AddOutputFilterByType DEFLATE application/xhtml+xml
#  AddOutputFilterByType DEFLATE application/xml
#  AddOutputFilterByType DEFLATE font/opentype
#  AddOutputFilterByType DEFLATE font/otf
#  AddOutputFilterByType DEFLATE font/ttf
#  AddOutputFilterByType DEFLATE image/svg+xml
#  AddOutputFilterByType DEFLATE image/x-icon
 #  AddOutputFilterByType DEFLATE text/css
 #  AddOutputFilterByType DEFLATE text/html
#  AddOutputFilterByType DEFLATE text/javascript
 #  AddOutputFilterByType DEFLATE text/plain
 #  AddOutputFilterByType DEFLATE text/xml
 #
 #  # Remove browser bugs (only needed for really old browsers)
 #  BrowserMatch ^Mozilla/4 gzip-only-text/html
 #  BrowserMatch ^Mozilla/4\.0[678] no-gzip
 #  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
 #  Header append Vary User-Agent
 #</IfModule>
 #
 ### EXPIRES CACHING ##
 #<IfModule mod_expires.c>
 #  ExpiresActive On
 #
 #  # Images
 #  ExpiresByType image/jpeg "access plus 1 year"
 #  ExpiresByType image/gif "access plus 1 year"
 #  ExpiresByType image/png "access plus 1 year"
 #  ExpiresByType image/webp "access plus 1 year"
 #  ExpiresByType image/svg+xml "access plus 1 year"
 #  ExpiresByType image/x-icon "access plus 1 year"
 #
 #  # Video
 #  ExpiresByType video/mp4 "access plus 1 year"
 #  ExpiresByType video/mpeg "access plus 1 year"
 #
 #  # CSS, JavaScript
 #  ExpiresByType text/css "access plus 1 month"
 #  ExpiresByType text/javascript "access plus 1 month"
 #  ExpiresByType application/javascript "access plus 1 month"
 #
 #  # Others
 #  ExpiresByType application/pdf "access plus 1 month"
 #  ExpiresByType application/x-shockwave-flash "access plus 1 month"
 #</IfModule>
 ### EXPIRES CACHING ##

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

RewriteEngine On

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

 #    # Force Https
 #    RewriteEngine On
 #    RewriteCond %{HTTPS} !=on
 #    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
 #
 #    # Force www
 #    RewriteCond %{HTTP_HOST} !^www\. [NC]
 #    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# 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>

 # Check if mod is installed. If not, you can try to comment the condition
 <IfModule mod_php7.c>
   php_value post_max_size 200M
   php_value upload_max_filesize 200M
   php_value memory_limit 300M
   php_value max_execution_time 259200
   php_value max_input_time 259200
   php_value session.gc_maxlifetime 1200
  </IfModule>

  <IfModule mod_autoindex.c>
    Options -Indexes
        </IfModule>
  <FilesMatch "^\.">
Order allow,deny
Deny from all
</FilesMatch>

【问题讨论】:

  • 您的.htaccess 文件在哪里?

标签: php laravel apache .htaccess mod-rewrite


【解决方案1】:

如果您的.htaccess 文件位于/project-name 子目录而不是文档根目录中,那么是的,问题与.htaccess 有关。具体来说,删除尾部斜杠的以下规则:

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

如果.htaccess 文件在/project-name 子目录中,那么$1 反向引用将排除子目录,因为RewriteRule 匹配的URL 路径是相对于当前的模式文件系统目录。

您需要从 REQUEST_URI 服务器变量中捕获 URL 路径(减去尾部斜杠),该变量包含完整的根相对 URL 路径。

请尝试以下方法:

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

注意 substitution 字符串中的 %1 反向引用,而不是之前的 $1%n 形式的反向引用与前面的条件相关。

由于这是一个 301(永久)重定向,因此您需要在测试前清除浏览器缓存。最好使用 302(临时)重定向进行测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-27
    • 2015-08-27
    • 2015-10-23
    • 2019-08-15
    • 2014-05-17
    • 2014-07-30
    • 2016-06-02
    相关资源
    最近更新 更多