htaccess如何将URL参数重定向到子文件夹?

3
我想将所有请求重定向到我的public/index.php文件。我已经尝试过:
RewriteEngine On 
    
RewriteRule ^(.*)$ public/index.php?url=$1

代码看起来没问题但运行不了。我的目标是将链接从http://example.com/?url=user/profile/2改为http://example.com/user/profile/2

我的目录结构如下:

  • 根目录

    • public
      • index.php
    • vendor
    • .htaccess
    • composer.json
2个回答

1
这是解决方案: 在Apache2的httpd.conf文件中取消注释(删除#mod_rewrite.so模块(在我的情况下是c:\Apache24\conf\httpd.conf文件)。还需在该文件中,在DocumentRoot "${SRVROOT}/htdocs"部分下,将AllowOverride none更改为AllowOverride All。请保留html标签。
以下是.htaccess内容。
RewriteEngine on

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

然而,如果您需要在静态目录中获取静态文件,请在静态目录内添加一个新的 .htaccess 文件,并包含 RewriteEngine off 内容。


1
请尝试在您的.htaccess文件中使用以下规则集来处理类似于:http://example.com/user/profile/2 的URL。将您的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]


或者尝试以下规则,请确保同时只尝试上述规则或下面的规则。

上述规则适用于不存在的页面,如果要使其适用于任何URL(如您在问题中提到的),请尝试:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接