Page 1 of 2
Mod_Rewrite Questions...
Posted: Sat Jul 22, 2006 2:56 pm
by tecktalkcm0391
Where would I put this code so that it only affects the folder site.com/folder:
Code: Select all
RewriteEngine On
RewriteBase /
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
And would that just remove the .php... I got it off of another site.
Also it there a way to make it so that if the files in a folder are requsted by site.com it returns a 404 error page and then if its requsted by the site domain.com. it allows it. See I am hosted by ipower and I have a domain pointer to my public html folder and then to a folder. I want it to be sot that when I am on the regular site you can't get to the folder, but when I am on the pointed domain it shows up everything.
Posted: Sat Jul 22, 2006 3:16 pm
by tecktalkcm0391
OK I found to put it in .htaccess file, but I am having a problem if I go to site.com/folder/index.php it sends me to site.com/... and when I got to site.com/folder/dir/dir/file.php nothing comes up at all... help!
Posted: Sat Jul 22, 2006 3:17 pm
by RobertGonzalez
Put the .htaccess file in the folder that you want to be affected by it.
You can send any request to your .htaccess file and return a 403. You will need to know which files and enter them by name or use some regex matching on file types.
Posted: Mon Jul 24, 2006 9:01 am
by tecktalkcm0391
I put that in the .taccess file and I am gettin so manyproblems... If I go to /index.php i get errors, and i just can't get anythign to work. And if i go to /folder/dir/folder/file.php i get and error as well as folder/dir/folder/file
Posted: Tue Jul 25, 2006 1:59 am
by RobertGonzalez
I was just looking... none of your rewrite rules are going to work because they have no opening delimiter (^). Have you looked at the mod_rewrite cheat sheet in the Apache, IIS and Servers forum?
Posted: Tue Jul 25, 2006 11:08 am
by tecktalkcm0391
Yes, and I don't understand a thing about them.
Posted: Wed Jul 26, 2006 12:53 am
by RobertGonzalez
Alright, you speak it in english and I will try to translate it into geek. What exactly are you wanting to do?
Posted: Wed Jul 26, 2006 9:44 am
by tecktalkcm0391
I want to remove .php for the URL the user sees but where in the codes I still can call something.php and link to something.php.
If its possible I want to be able to do this:
I have a shared hosted domain, site.com. I also have a domain, example.com domain pointed to site.com/folder. I want to make it so that if someone goes to site.com/folder it returns a 404 error. If they go to example.com it doesn't do anything and it allows them to view the pages.
THANKS! 
Posted: Wed Jul 26, 2006 11:37 pm
by RobertGonzalez
tecktalkcm0391 wrote:If its possible I want to be able to do this:
I have a shared hosted domain, site.com. I also have a domain, example.com domain pointed to site.com/folder. I want to make it so that if someone goes to site.com/folder it returns a 404 error. If they go to example.com it doesn't do anything and it allows them to view the pages.
So you have something like this...
Code: Select all
example.com ---\ site.com
\----> /folder
The challenge is that if you lock site.com/folder down and return a 403 forbidden, then pointing example.com to it will return the same 403 forbidden. Doesn't that kind of defeat the purpose?
Posted: Thu Jul 27, 2006 9:31 am
by tecktalkcm0391
Yeah I guess so I didn't know if you could make a rule like:
If site X requests pages Allow
If site Y requests pages Deny
But I guess your right.
How would I remove .php for the URL the user sees but where in the codes I still can call something.php and link to something.php.
Posted: Thu Jul 27, 2006 10:20 am
by tecktalkcm0391
For the allowing and not allowing can't I just do:
Code: Select all
Allow from apache.org
Deny from .net example.edu
I found that on:
http://httpd.apache.org/docs/2.2/mod/mo ... _host.html
Posted: Thu Jul 27, 2006 10:28 am
by RobertGonzalez
Yeah, I think you can do it that way, but I think the Allow From directive will then take precedent, meaning that if the referer is not THAT domain, your page won't load. I could be way wrong on this though.
As for the dropping of the .php extension, mod_rewrite can do that easily. Essentially you tell your .htaccess file to take a certain pattern and transform that pattern behind the scenes to your actual pages.
Code: Select all
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [S=1]
RewriteRule ^(.+)/$ $1.php [QSA,L]
Posted: Thu Jul 27, 2006 10:30 am
by tecktalkcm0391
Great I'll try that.
Right Now I have:
Code: Select all
RewriteEngine on
RewriteRule ^us/([A-Z]+)$ us.php?state=$1 [nocase]
I want that to take anything that is us.php?state=[State] and convert it to us/[State]
how come when I go the the us.php?state=$1 it stays the same?
Posted: Thu Jul 27, 2006 10:34 am
by tecktalkcm0391
Ok, my current .htaccess file has:
Code: Select all
ErrorDocument 401 error.php?error=401
ErrorDocument 403 error.php?error=403
ErrorDocument 404 error.php?error=404
ErrorDocument 500 error.php?error=500
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [S=1]
RewriteRule ^(.+)/$ $1.php [QSA,L]
RewriteRule ^us/([A-Z]+)$ us.php?state=$1 [nocase]
And nothing seems to be working I keep getting the 404 error when I got to site.com/dir/index instead of index.php if i go to index.php it still displays index.php.
Sorry if I am being a pain but I can't seem to figure this out!

Posted: Fri Jul 28, 2006 1:07 am
by RobertGonzalez
tecktalkcm0391 wrote:Great I'll try that.
Right Now I have:
Code: Select all
RewriteEngine on
RewriteRule ^us/([A-Z]+)$ us.php?state=$1 [nocase]
I want that to take anything that is us.php?state=[State] and convert it to us/[State]
how come when I go the the us.php?state=$1 it stays the same?
mod_rewrite does not work that way. If you type in site.com/us/[State] where State is the name of a state, then it would look like the clean URL but would actually load site.com/us.php?state=[State]