[mod_rewrite] preventing direct access to .php files

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
nutkenz
Forum Contributor
Posts: 155
Joined: Tue Jul 19, 2005 12:25 pm

[mod_rewrite] preventing direct access to .php files

Post by nutkenz »

I'd like to make only 1 possible path to a certain website to prevent pagerank being divided, here is the rule which is supposed to accomplish this;

RewriteBase /
RewriteRule ^([a-zA-Z0-9]+)\.php$ $1 [L]

The problem is that this rule seems to cause an infinite loop redirecting to itself over and over again... Maybe I'm too tired right now, but I can't think of another way to get it working. Does anyone else have an idea?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

RewriteRule ^(.+)?$ /index.php?p=$1 [QSA,L]
This takes just about any URL to your site and redirects it to index.php?p=WhatWasEntered.
nutkenz
Forum Contributor
Posts: 155
Joined: Tue Jul 19, 2005 12:25 pm

Post by nutkenz »

Everah wrote:

Code: Select all

RewriteRule ^(.+)?$ /index.php?p=$1 [QSA,L]
This takes just about any URL to your site and redirects it to index.php?p=WhatWasEntered.
Won't this also cause a loop?

index.php?p=WhatWasEntered
index.php?index.php?p=WhatWasEntered
index.php?index.php?index.php?p=WhatWasEntered

If not; why does my rule (RewriteRule ^([a-zA-Z0-9]+)\.php$ $1 [L] ) cause a loop?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Well damn, yes it does. Sorry about that. Try this one...

Code: Select all

RewriteRule ^(.*)$ index.php?p=$1 [R,NC,L]
This takes anything that comes to your site and redirects ([R]) them to your other selected page. This will not cause infinitie loops with the R flag.

PS, I got this from the cheatsheet in the Apache, IIS, Web Servers that is listed in the Useful Resources thread.
nutkenz
Forum Contributor
Posts: 155
Joined: Tue Jul 19, 2005 12:25 pm

Post by nutkenz »

Yes, but then the user sees the .php file which is exactly what I don't want. I only want one possibility so links are always consistent...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

nutkenz wrote:Yes, but then the user sees the .php file which is exactly what I don't want. I only want one possibility so links are always consistent...
Explain this a little better. I'm not quite understanding what you mean here.
nutkenz
Forum Contributor
Posts: 155
Joined: Tue Jul 19, 2005 12:25 pm

Post by nutkenz »

Everah wrote:
nutkenz wrote:Yes, but then the user sees the .php file which is exactly what I don't want. I only want one possibility so links are always consistent...
Explain this a little better. I'm not quite understanding what you mean here.
Right now people can use both http://www.domain.tld/page and http://www.domain.tld/page.php, I want only http://www.domain.tld/page to be available, if possible.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Not gonna happen, as far as I know. mod_rewrite can rewrite a url so that the user thinks they are on a page when they are actually on another page. But I am not sure that you can totally prevent someone from going to the page that actually exists. I could be wrong though.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

http://httpd.apache.org/docs/1.3/mod/mo ... ewriteCond talks about "-f" and such.. hmmmm..
Post Reply