简体   繁体   中英

Link Duplicity problem with .htaccess and mod_rewrite

Sorry I couldn't come up with a better title.

Here is my htaccess

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L,QSA]

And this is my problem:

localhost/validpage gives me the contents of localhost/validpage.php. localhost/validpage/blah also gives me the same (validpage.php) page and so does localhost/validpage/blah/blah/... Therefore the problem I am facing is link duplicity (in my words!).

How do I allow localhost/validpage.php to be accessed from localhost/validpage only and nothing else, not even localhost/validpage.php .

I have started a question on *Server****Fault*** too but with not much success. The answer I have got is it cannot be done with htaccess alone.

By validpage I mean any valid page on the server. Since I am retrofitting an existing site with mod_rewrite for cleaner urls, I am looking for a relatively easy solution preferably with .htaccess only. However, any solutions are welcome.

what is the source attribute of your images, etc ???
absolute or relative?

<img src="/images/my.jpg" /> and <img src="images/my.jpg" /> point to different files when applying your rewrite rules.

You could try using this in your .htaccess

RewriteEngine on
RewriteBase /

# if request has a php extension remove and redirect
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^((.*)\.php)$
RewriteRule ^(.*).php$ $1 [L,R=301]

# if request uri has no extension link to php file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

I should rewrite your php scripts to friendly urls, and redirect requests using the .php extension.

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