简体   繁体   中英

htaccess rewritecond not working

I hope you can help. I'm trying to write a htaccess file do the following.

1) redirect to www. address

2) remove .php from url

3) If the file doesn't exist then use filechecker.php?page=filename


1 I can do with

RewriteCond %{HTTP_HOST} ^example.com$

RewriteRule (.*) http://www.example.com/ $1 [R=301,L]


2) I can do with

RewriteCond %{SCRIPT_FILENAME}.php -f

RewriteRule [^/]$ %{REQUEST_URI}.php [QSA,L]


3) I thought

RewriteCond %{REQUEST_FILENAME}.php !-f

RewriteRule ^([^/]*)$ filechecker.php?page=$1 [QSA,L]

would work, but for some reason it is ignoring the fact that the page does actually exist.


I hope you can help Mark

Your solution for 2 will loop, but simple enough to fix, stick something along the following lines in your file for part 2 and 3:

#If the Browser request contains a .php, instruct the browser to remove it.
RewriteCond %{THE_REQUEST}      \.php      [NC]
RewriteRule ^/?(.*)\.php$       http://%{HTTP_HOST}/$1         [R=301,NC,L]

#If a request is received for a non file-system object, that doesn't have a .php suffix, then store the full path, filename, and URI in a variables with that extention.
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-s
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !^\.php$  [NC]
RewriteRule ([^/]+)$      -  [E=testScript:%{SCRIPT_FILENAME}.php,E=testFile:$1.php,E=testURI:%{REQUEST_URI}.php]

#See if the file exists with a .php extention, if it does internally rewrite
RewriteCond %{ENV:testScript} -s  [OR]
RewriteCond %{ENV:testScript} -f
RewriteRule .*              %{ENV:testURI} [L]

#Else if a ENV:testDile is set, then pass the name to the php script
RewriteCond %{ENV:testFile} !^$
RewriteRule .*               /filechecker.php?page=%{ENV:testFile} [L]

FYI: The Apache mod_rewrite documentation is worth a read, if your unclear as to what the above rules do, or if you want something a little more abstract consider the following post .

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