Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache 浏览器似乎显示由.htaccess提供的查询_Apache_.htaccess_Url_Redirect_Mod Rewrite - Fatal编程技术网

Apache 浏览器似乎显示由.htaccess提供的查询

Apache 浏览器似乎显示由.htaccess提供的查询,apache,.htaccess,url,redirect,mod-rewrite,Apache,.htaccess,Url,Redirect,Mod Rewrite,我用来将所有内容重定向到index.php的方法似乎有问题,index.php位于我的根文件夹public_html中。当然,也不应该有例外,除非文件夹关闭了重写引擎。为了简单起见,让我们假设我使用www.example.com作为我的网站 我的.htaccess如下所示: RewriteEngine on RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] 例如,假设在浏览器中输入:www.example.com/test 它按照预期在内部重定向到:ww

我用来将所有内容重定向到index.php的方法似乎有问题,index.php位于我的根文件夹public_html中。当然,也不应该有例外,除非文件夹关闭了重写引擎。为了简单起见,让我们假设我使用www.example.com作为我的网站

我的.htaccess如下所示:

RewriteEngine on
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
  • 例如,假设在浏览器中输入:www.example.com/test
  • 它按照预期在内部重定向到:www.example.com/index.php?url=test
问题是:

  • 我希望浏览器仍然显示:www.example.com/test
  • 但是,浏览器现在显示:www.example.com/test/?url=test
只有在根目录中有一个名为test的文件夹时,才会显示这个混乱的名称。如果我删除该文件夹,它将显示:

  • www.example.com/test
但是,如果我再次添加文件夹,它会显示:

  • www.example.com/test/?url=test
它仍然可以正确重定向,但看起来不再干净。 我好奇的是到底是什么原因导致它这样做,以及如何解决/防止它

请注意,我总是想重写url。因此,我不想为文件夹设置例外。无论是否有文件夹,它的外观和工作方式都应该相同


编辑1:根文件夹将是public_html,so example.com(作为示例)


编辑2:修改了问题的措辞,问题仍然是一样的


编辑3:更改了问题的格式



编辑4:添加了我想重写url的内容,无论是否存在文件夹。

您需要在
中使用此内容。htaccess

RewriteEngine On
RewriteRule ^([^/]*)$ /test/?url=$1 [L]

此代码将为您留下所需的URL。确保在测试缓存之前清除它。

您需要在
中使用它。htaccess

RewriteEngine On
RewriteRule ^([^/]*)$ /test/?url=$1 [L]

此代码将为您留下所需的URL。在测试缓存之前,请确保清除缓存。

这可能需要进一步的编辑才能完全按照您的要求执行,但使用
RewriteCond
可以指定何时实际重写url的条件

RewriteEngine On
RewriteBase /
 ##perform rewrite only if requested file does not exist...
RewriteCond %{REQUEST_FILENAME} !-f
 ##perform rewrite only if requested directory does not exist...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /test/?url=$1 [L]

这可能需要进一步的编辑才能完全完成您想要的操作,但是使用
RewriteCond
可以指定何时实际重写url的条件

RewriteEngine On
RewriteBase /
 ##perform rewrite only if requested file does not exist...
RewriteCond %{REQUEST_FILENAME} !-f
 ##perform rewrite only if requested directory does not exist...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /test/?url=$1 [L]

谢谢你的回答,不幸的是它似乎不起作用。我把问题的措辞和形式改得更具体了。谢谢你的回答,不幸的是它似乎不起作用。我改变了问题的措辞和格式,使之更加具体。目前我使用的解决方案是添加一个尾随斜杠。因此,www.example.com/test将首先转换为www.example.com/test/。使用尾随斜杠时,它不会显示/?url=测试。然而,我仍然不知道为什么它首先显示url参数。目前我使用的解决方案是添加一个尾随斜杠。因此,www.example.com/test将首先转换为www.example.com/test/。使用尾随斜杠时,它不会显示/?url=测试。然而,我仍然不知道为什么它首先显示url参数。