【问题标题】:htaccess redirect to subfolder with url paramethershtaccess 重定向到带有 url 参数的子文件夹
【发布时间】:2021-07-19 09:13:21
【问题描述】:

我想将每个请求重定向到我的 public/index.php 文件。我试过了

RewriteEngine On 
    
RewriteRule ^(.*)$ public/index.php?url=$1

看起来不错但无法正常工作的代码。我的目标是将网址形式 http://example.com/?url=user/profile/2 更改为 http://example.com/user/profile/2。 我的目录结构是

    • 公开
      • index.php
    • 供应商
    • .htaccess
    • composer.json

【问题讨论】:

    标签: .htaccess mod-rewrite url-rewriting query-string friendly-url


    【解决方案1】:

    要处理像:http://example.com/user/profile/2 这样的 URL,请尝试在您的 .htaccess 文件中遵循规则集。将您的 htaccess 规则文件放入根目录。请确保在测试您的 URL 之前清除您的浏览器缓存。

    Options -MultiViews
    RewriteEngine ON
    RedirectBase /ApadanaCMS/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]*)/([^/]*)/(.*)/?$ public/index.php?url=$1/$2/$3 [L]
    


    或尝试遵循规则,请确保一次仅尝试上述规则或遵循规则。

    以上将适用于不存在的页面,使其适用于任何网址(就像您在问题中提到的那样)尝试:

    Options -MultiViews
    RewriteEngine ON
    RedirectBase /ApadanaCMS/
    RewriteCond %{REQUEST_FILENAME} !index\.php [NC]
    RewriteRule ^([^/]*)/([^/]*)/(.*)/?$ public/index.php?url=$1/$2/$3 [L]
    

    【讨论】:

      【解决方案2】:

      解决方案如下: 取消注释(删除 #mod_rewrite.so apache2(在我的情况下为 c:\Apache24\conf\httpd.conf)文件中 httpd.conf 文件中的模块。还将AllowOverride none 更改为AllowOverride All 在该文件中的DocumentRoot "${SRVROOT}/htdocs" 部分下。

      这是 .htaccess 内容

      RewriteEngine on
      
      RewriteRule ^(.*)$ public/index.php?url=$1 [QSA,L]
      RewriteRule ^()$ public/index.php?url=$1 [QSA,L]
      

      但是,如果您需要在静态目录中获取静态文件,请在静态目录中添加一个带有 RewriteEngine off 内容的新 .htaccess 文件

      【讨论】:

        猜你喜欢
        • 2014-06-22
        • 2021-08-05
        • 2017-08-17
        • 1970-01-01
        • 2022-07-01
        • 1970-01-01
        • 2015-04-09
        • 2014-06-01
        相关资源
        最近更新 更多