简体   繁体   中英

trying to rewrite url via htaccess only in specific case

For the domain www.example.com, I would like to direct the user to index.php. Using

DirectoryIndex index.php

I do this. However, if they include a slash and a non-file-type value, I want to redirect them to a different page and use the value as a php get variable:

www.example.com/hello lands on a_different_page.php?var=hello

But I don't want to disturb a request ending in "php" for example:

www.example.com/page.php

should not try to redirect. And also a request for the plain url should not get changed:

www.example.com goes to index.php

I have found ways to make some of these happen, but they always interfere with other requests as an unintended consequence. Any help would be greatly appreciated - my research on htaccess re-writes has not helped.

The best way to deal with this, is to not redirect anything that points to something that already exists.

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule (.*) a_different_page.php?var=$1

You can use the following htaccess :

DirectoryIndex index.php
RewriteEngine on
#rewrite /foobar unless a real file and dir to /page.php?var=foobar
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) /page.php?var=$1 [L]

Hope it will work fine. Working perfectly okay for me.

RewriteEngine On

Options -Multiviews
#For rewriting / request to index.php
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule ^(.*)$ /index.php [L]

#For redirecting /some/anonymous/request to some_different_page.php?var=/some/anonymous/request when request uri does not ends with .php
RewriteCond %{REQUEST_URI} !\.(php)$
RewriteRule ^(.*)$ /some_different_page.php?var=$1 [R=301,L,QSA]

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