Mod_Rewrite Questions...

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

User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Mod_Rewrite Questions...

Post 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.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Yes, and I don't understand a thing about them.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Alright, you speak it in english and I will try to translate it into geek. What exactly are you wanting to do?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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! :D
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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]
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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! :oops: :? :cry:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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]
Post Reply