【问题标题】:htaccess rewrite url with eventual subfolderhtaccess 用最终的子文件夹重写 url
【发布时间】:2021-10-17 10:19:15
【问题描述】:

我有这样的网址:

https://mywonderfulsite.com/page

我的 .htaccess 将其重写为

https://mywonderfulsite.com/index.php?source=page

.htaccess 如下:

RewriteEngine on
RewriteBase /
RewriteRule \.(php)$ - [L]
RewriteRule ^index\.html$ - [L]
RewriteRule ^favicon\.ico$ - [L]
RewriteRule ^custom404\.html$ - [L]
RewriteRule ^custom500\.html$ - [L]
RewriteRule ^([^/]+)/?$ index.php?source=$1 [L]

如果 url 包含类似的子文件夹,则会失败

https://mywonderfuldomain.com/subfolder/anotherpage

应该变成

https://mywonderfuldomain.com/index.php?source=/subfolder/anotherpage

我似乎不知道如何处理这种情况。如何解决?

【问题讨论】:

    标签: apache .htaccess mod-rewrite apache2.4


    【解决方案1】:

    问题在于模式 ([^/]+) 匹配除 / 之外的任何内容。

    像这样使用它:

    RewriteEngine On
    
    RewriteRule \.php$ - [L,NC]
    
    RewriteRule ^(?:favicon\.ico|(?:index|custom500|custom404)\.html)$ - [L,NC]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule .+ index.php?source=$0 [L,QSA]
    

    【讨论】:

      猜你喜欢
      • 2012-08-20
      • 2015-04-22
      • 2023-03-26
      • 2015-09-27
      • 2021-09-16
      • 2014-03-20
      • 1970-01-01
      • 2017-07-16
      • 1970-01-01
      相关资源
      最近更新 更多