简体   繁体   中英

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

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 )

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. 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

If the htaccess file is in the versioned directory, you don't want the leading slash in the rule's target:

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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