TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-16-2008, 03:29 PM   #1 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default Mod_rewrite

I'm trying to use mod_rewrite on a site I'm doing. My problem is I want to have a few different rules and I'm not sure how to do them at all

For example, if somebody goes to:
I need to redirect it to:
But I also want if someone goes to:
to send them to:
Is it possible to have lots of different rules like this or will they all just confuse each other?

If someone could point me to a good article on mod_rewrite aswell I'd be grateful :)
__________________
Free CSS Tutorials
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 06-16-2008, 06:29 PM   #2 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

Mod_Rewrite Forums :: Index
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 06-16-2008, 08:36 PM   #3 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

That is another probably good place to ask questions, but here's a few places to find answers:
mod_rewrite Cheat Sheet - Cheat Sheets - ILoveJackDaniels.com
Easy Mod Rewrite : mod_rewrite tutorial
mod_rewrite: A Beginner's Guide to URL Rewriting [Apache & IIS Configuration]

I can give you a hint too, you're going to want to use a RewriteCond for the first one you want to do, and a relatively easy RewriteRule for the second. It's possible to have almost as many rules as you want, though it is easy to run into conflicts. The rules cascade though, so it'll go through them one by one in order until it finds one that matches.
-m
delayedinsanity is offline  
Reply With Quote
Old 06-17-2008, 08:11 AM   #4 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default

Thanks for the links. I've had a look at the tutorials but they dont seem to go into the RewriteCond bit in much detail.

I'm guessing I should be using something along the lines of:
Code:
RewriteCond % (HTTP_HOST)
But then what do I do?
__________________
Free CSS Tutorials
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 06-17-2008, 04:02 PM   #5 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

I'm guessing for the first one something along the lines of

Code:
RewriteCond %{HTTP_HOST} ([^.]+)\.mysite\.com [NC]
RewriteRule ^(.*) http://www.mysite.com/user/index.php?user=%1 [P, L]
...might be what you want, and the second...

Code:
RewriteRule ^features/$ features.php [L]
You could also use a small regular expression to easily make the PHP extension invisible on an entire directory of files;

Code:
RewriteRule ^([A-Za-z0-9]+)/$ $1.php [L]
-m
delayedinsanity is offline  
Reply With Quote
Old 06-17-2008, 05:58 PM   #6 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default

I think I've worked out how to do the first part but apparently I need to enable the dns wildcard on subdomains on my hosting. Anybody got any experience with this?
__________________
Free CSS Tutorials
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 06-17-2008, 06:30 PM   #7 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

You'll have to update your zone DB file to include something like this:

Code:
*.mysite.com. 14400 IN A 12.345.67.890
...where mysite is your domain and 12.34... is your IP, and then you need to add two directives to your <VirtualHost> configuration in httpd.conf, such as;

Code:
ServerAlias *.mysite.com
RedirectMatch 301 (.*) http://mysite.com$1
-m
delayedinsanity is offline  
Reply With Quote
Old 06-18-2008, 12:33 AM   #8 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
For example, if somebody goes to:
Quote:
http://username.mysite.com

You cannot just do that with mod_rewrite, you must modify the virtualhosts in the apache config.
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 07-12-2008, 09:39 AM   #9 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default

Right, I've enabled the wildcard on subdomains on my host but I'm still having a problem with the .htaccess file.

I have three possible solutions, none of which work properly.

Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^(www\.)?mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^(www\.)?([^.]+).*<->/([^/]+) [NC]
RewriteCond %2<->%3 !^(.*)<->\1$ [NC]
RewriteRule ^(.+) /%2/$1 [L]
This one works fine but it redirects myname.mydomain.com to mydomain.com/myname when I need it to redirect to mydomain.com/user/index.php?user=myname and I'm not sure how to change it.

Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^w{3}\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^/$ /user/index.php?user=%1 [L]
This one just redirects myname.mydomain.com to mydomain.com.

Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ([^.]+)\.mydomain\.com [NC]
RewriteRule ^(.*) http://www.mydomain.com/user/index.php?user=%1 [L]
This one I get an error in the error log saying bad flag delimiters.
__________________
Free CSS Tutorials
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:41 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design