Page 1 of 1

[mod_rewrite] preventing direct access to .php files

Posted: Sat Aug 12, 2006 2:28 pm
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?

Posted: Sun Aug 13, 2006 12:56 am
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.

Posted: Sun Aug 13, 2006 3:20 am
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?

Posted: Sun Aug 13, 2006 3:29 am
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.

Posted: Sun Aug 13, 2006 3:52 am
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...

Posted: Sun Aug 13, 2006 10:33 am
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.

Posted: Mon Aug 14, 2006 5:07 am
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.

Posted: Mon Aug 14, 2006 12:22 pm
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.

Posted: Mon Aug 14, 2006 12:41 pm
by feyd
http://httpd.apache.org/docs/1.3/mod/mo ... ewriteCond talks about "-f" and such.. hmmmm..