TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Mod_rewrite (http://www.talkphp.com/general/2968-mod_rewrite.html)

Jmz 06-16-2008 03:29 PM

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 :)

xenon 06-16-2008 06:29 PM

Mod_Rewrite Forums :: Index

delayedinsanity 06-16-2008 08:36 PM

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

Jmz 06-17-2008 08:11 AM

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?

delayedinsanity 06-17-2008 04:02 PM

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

Jmz 06-17-2008 05:58 PM

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?

delayedinsanity 06-17-2008 06:30 PM

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

Orc 06-18-2008 12:33 AM

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.

Jmz 07-12-2008 09:39 AM

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.


All times are GMT. The time now is 05:52 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0