简体   繁体   English

htaccess重定向在相对路径中不起作用

[英]htaccess redirects not working in relative paths

I'm trying to make a redirection from all files to a template file. 我正在尝试从所有文件重定向到模板文件。 But redirects only works when I specify an URL 但是重定向仅在我指定URL时有效

For example: 例如:

Does not work (it does not redirects) 不起作用(不重定向)

Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /template.php?page=$1 [L,QSA]

Works (redirects to http://domain.com/versioned/template.php?page=index.html ) 有效(重定向到http://domain.com/versioned/template.php?page=index.html

Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) http://domain.com/versioned/template.php?page=$1 [L,QSA]

And the second option is change the URL in the browser. 第二个选项是在浏览器中更改URL。 I need to keep the original URL. 我需要保留原始网址。

Thanks 谢谢

The problem is with 问题出在

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} -f

If I quit this, it works, but instead of returning the current file in URL, it returns always template.php 如果我退出,它可以工作,但是它不会返回URL中的当前文件,而是始终返回template.php

If the htaccess file is in the versioned directory, you don't want the leading slash in the rule's target: 如果htaccess文件位于versioned目录中,则您不希望规则目标中的斜杠为:

Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) template.php?page=$1 [L,QSA]

The relative URL-path in this rule's target, template.php?page=$1 , tells mod_rewrite to rewrite the request to this URL-path that is within the path that the htaccess file is in. When you had the target as /template.php?page=$1 , it assumed an absolute URL-path, which would be equivalent to: http://domain.com/template.php?page=foo . 该规则目标中的相对URL路径template.php?page=$1告诉mod_rewrite将请求重写到此htaccess文件所在路径中的URL路径。当您将目标作为/template.php?page=$1 ,它假定使用绝对URL路径,该路径等效于: http://domain.com/template.php?page=foo : http://domain.com/template.php?page=foo /template.php?page=$1 http://domain.com/template.php?page=foo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM