Forum Moderators: phranque

Message Too Old, No Replies

htaccess to subdomain and dynamic urls

         

karlpox

1:48 pm on Dec 3, 2008 (gmt 0)

10+ Year Member



# Rewrite subdomain requests to subdirectories except for www.exmaple.com
RewriteEngine On

#RewriteCond %{REQUEST_URI} !^/sd_
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
#RewriteRule (.*) /sd_%1/$1 [L]
RewriteRule (.*) /index.php?id=%1 [L]
#
# Redirect direct user-agent requests for www.example.com/sd_<subdomain>/<page> to [<subdomain>.example.com...]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /sd_(.+)\ HTTP/
RewriteRule ^sd_([^/]+)/(.*)$ [$1.example.com...] [R=301,L]

got this code from this link [webmasterworld.com...]

everything is kinda working with the redirect

so if i access [admin.example.com...] it goes to http://example.com/index.php?id=admin

i wanted to add some dynamic urls into it. how can i do this

[admin.example.com...] -> http://example.com/index.php?id=admin&action=profile

and

[admin.example.com...] -> http://example.com/index.php?id=admin&action=gallery&album=album1

thanks in advance

-karl

jdMorgan

12:56 am on Dec 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I understand you correctly, you want to append the "id=<subdomain>" to any existing query string, preserving any query data sent by the cleint, rather than completely replacing it with the "id=<subdomain>" query string.

If that is the case, simply add the [QSA] flag to the the first rule, making it "[QSA,L]". See the Apache mod_rewrite documentation for details.

Jim

karlpox

11:11 am on Dec 4, 2008 (gmt 0)

10+ Year Member



which part should i add the QSA? in this line?
RewriteRule (.*) /index.php?id=%1 [L]

i tried adding it to that line and when i visit http://example.com or http://www.example.com it goes to index.php?id=www or index.php?id=

what if i want to add more dynamic urls to it except subdomain

how can i add this line http://example.com/register = index.php?action=register ?

will this do fine?
RewriteRule ^/?([a-zA-Z0-9_]+)$ index.php?action=$1 [L]

karlpox

11:41 am on Dec 4, 2008 (gmt 0)

10+ Year Member



by using the htaccess on my first post i could then go to karlpox.example.com and will be transfered to example.com/index.php?id=karlo and i found a way to translate trailing subfolders using some functions in php.

how can i make this redirect
http://example.com/register -> index.php?action=register

what line do i have to add?

jdMorgan

1:19 pm on Dec 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Delete or comment-out the redirect (second rule) for now -- It is incorrect and needs to be further modified to work with your set-up. For best results, debug one rule at a time.

If I understand what you're trying to accomplish, then here are several modified internal rewrite rules to handle the four possible cases of subdomains and "actions":


# Rewrite requests for <subdomain>.example.com/<action>
RewriteCond $1 !^index\.php$
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^([^/]+)$ /index.php?id=%1&action=$1 [L]
#
# Rewrite requests for <subdomain>.example.com/
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^$ /index.php?id=%1 [L]
#
# Rewrite requests for example.com/<action> or www.example.com/<action>
RewriteCond $1 !^index\.php$
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule ^([^/]+)$ /index.php?action=$1 [L]
#
# Rewrite requests for example.com/ or www.example.com/
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule ^$ /index.php? [L]

Jim

karlpox

4:29 pm on Dec 4, 2008 (gmt 0)

10+ Year Member



wooot everything is working now. thanks alot jim. your code worked perfectly.

jdMorgan

9:05 pm on Dec 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, now you need to take care of the opposite direction: To prevent search engines and users from directly requesting /index.php?id=x&action=y from your server. If you allow this without redirecting, then you have a duplicate content problem; The same content available at two different URLs...

Jim

karlpox

5:18 am on Dec 17, 2008 (gmt 0)

10+ Year Member



hey jdmorgan

how can I make the htaccess you made to work in localhost? what do i have to replace?

g1smd

10:09 am on Dec 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Modify your HOSTS file to say

127.0.0.1  testsite.test

127.0.0.1  www.testsite.test

127.0.0.1  admin.testsite.test

Set up Apache to accept those requests, and then modify the local .htaccess file to use those names.

karlpox

2:45 pm on Dec 18, 2008 (gmt 0)

10+ Year Member



i modified my htaccess from mysite.com to localhost

# Rewrite requests for <subdomain>.example.com/<action>
RewriteCond $1 !^index\.php$
RewriteCond %{HTTP_HOST} !^localhost [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.localhost
RewriteRule ^([^/]+)$ /index.php?id=%1&action=$1 [L]
#
# Rewrite requests for <subdomain>.example.com/
RewriteCond %{HTTP_HOST} !^localhost [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.localhost
RewriteRule ^$ /index.php?id=%1 [L]
#
# Rewrite requests for example.com/<action> or www.example.com/<action>
RewriteCond $1 !^index\.php$
RewriteCond %{HTTP_HOST} ^localhost
RewriteRule ^([^/]+)$ /index.php?action=$1 [L]
#
# Rewrite requests for example.com/ or www.example.com/
RewriteCond %{HTTP_HOST} ^localhost
RewriteRule ^$ /index.php? [L]

somethings seems to be wrong. not working properly unlike if it is uploaded to my web host.

jdMorgan

3:12 pm on Dec 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did you preface this code with Options FollowSymLinks and RewriteEngine on?

The servers may have other configuration differences in httpd.conf and conf.d, etc. that affect RewriteRule processing, so check your local host configuration files.

Jim

karlpox

11:30 pm on Dec 18, 2008 (gmt 0)

10+ Year Member



nope just the RewriteEngine on. Its working fine in the web server.But in localhost it can only access one subfolder, if i typed in 2 subfolders it returns error folder does not exist.

jdMorgan

12:42 am on Dec 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not clear.

Please provide specific examples of what URLs you typed, working and not working.

Jim

karlpox

4:24 am on Dec 20, 2008 (gmt 0)

10+ Year Member



the original htaccess you created. i just want it to run on localhost

jdMorgan

2:34 pm on Dec 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK.

karlpox

5:10 am on Dec 21, 2008 (gmt 0)

10+ Year Member



so what do i have to modify? the htaccess i posted doesn't seem to work properly.

jdMorgan

5:34 am on Dec 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Still waiting for example test URLs, working and not working, as requested above...

If you want specific answers, please post specific questions. "Doesn't work" tells us nothing.

Thanks,
Jim

karlpox

2:01 pm on Dec 21, 2008 (gmt 0)

10+ Year Member



oh ok. so here is the original htaccess you made which was working perfectly for me on my web server which is online.

# Rewrite requests for <subdomain>.example.com/<action>
RewriteCond $1 !^index\.php$
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^([^/]+)$ /index.php?id=%1&action=$1 [L]
#
# Rewrite requests for <subdomain>.example.com/
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^$ /index.php?id=%1 [L]
#
# Rewrite requests for example.com/<action> or www.example.com/<action>
RewriteCond $1 !^index\.php$
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule ^([^/]+)$ /index.php?action=$1 [L]
#
# Rewrite requests for example.com/ or www.example.com/
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule ^$ /index.php? [L]

and i modified it to this.

# Rewrite requests for <subdomain>.example.com/<action>
RewriteCond $1 !^index\.php$
RewriteCond %{HTTP_HOST} !^localhost [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.localhost
RewriteRule ^([^/]+)$ /index.php?id=%1&action=$1 [L]
#
# Rewrite requests for <subdomain>.example.com/
RewriteCond %{HTTP_HOST} !^localhost [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.localhost
RewriteRule ^$ /index.php?id=%1 [L]
#
# Rewrite requests for example.com/<action> or www.example.com/<action>
RewriteCond $1 !^index\.php$
RewriteCond %{HTTP_HOST} ^localhost
RewriteRule ^([^/]+)$ /index.php?action=$1 [L]
#
# Rewrite requests for example.com/ or www.example.com/
RewriteCond %{HTTP_HOST} ^localhost
RewriteRule ^$ /index.php? [L]

im trying to make it work on localhost which is offline.

[localhost...] is working fine it gets redirected to index.phh?action=members

but [localhost...] is not working it says

Not Found

The requested URL /members/test was not found on this server.

i tried [test.localhost...] but i think thats not possible. or the htaccess is all mess up

g1smd

5:14 pm on Dec 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



*** is working fine it gets redirected to ***

Careful with the terminology! There's no redirect there. That's a rewrite.

I don't see any of your patterns

([^/]+)
catering for more than one folder level depth.

Something like

(([^/])+/)*
or
(([^/])+/)+
may work.

karlpox

12:43 am on Dec 22, 2008 (gmt 0)

10+ Year Member



oh im sorry. i mean it was rewrited. the htaccess was working fine if its online under a domain. but not working properly offline which is localhost.

jdMorgan

3:26 pm on Dec 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



g1smd is correct. There is no rule at all for a URL such as /admin/categories

You'll need a pattern like ^([^/]+/[^/]+)$ to detect those /admin/categories URL-paths, but you will need to decide what you want to do with each 'part' or the URL-path, or whether you want to pass the entire URL-path to your script as "action=". We can only guess here, since we don't know your site or how it works.

Jim

karlpox

9:45 pm on Dec 22, 2008 (gmt 0)

10+ Year Member



i just configured my domain to point to my localhost. and the admin/categories not working anymore. all i wanted to do was rewrite everything to index.php. for example

[mysite.com...] --> index.php
[mysite.com...] --> index.php
[mysite.com...] -> index.php

how can i do that? since only one subfolder can be rewrited to index.php with my current htaccess.

im rewriting everything to index.php since i would just use $_SERVER['REQUEST_URI'] to capture the subfolders.

karlpox

10:04 pm on Dec 22, 2008 (gmt 0)

10+ Year Member



i just modified this few lines in my htaccess

RewriteCond $1 !^index\.php$
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule ^([^/]+)$ /index.php?action=$1 [L]

to

RewriteCond $1 !^index\.php$
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule ^([^/]+/[^/]+/[^/]+)$ /index.php?action=$1 [L]

RewriteCond $1 !^index\.php$
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule ^([^/]+/[^/]+)$ /index.php?action=$1 [L]

RewriteCond $1 !^index\.php$
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule ^([^/]+)$ /index.php?action=$1 [L]

And is working properly except for my ajax codes. how do i fix this?

karlpox

10:36 pm on Dec 22, 2008 (gmt 0)

10+ Year Member



I tried adding this to the subdomain area in the htaccess

from this

# Rewrite requests for <subdomain>.example.com/<action>
RewriteCond $1 !^index\.php$
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^([^/]+)$ /index.php?id=%1&action=$1 [L]
#

to this

# Rewrite requests for <subdomain>.example.com/<action>
RewriteCond $1 !^index\.php$
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^([^/]+)$ /index.php?id=%1&action=$1 [L]
#
RewriteRule ^([^/]+/[^/]+)$ /index.php?id=%1&action=$1 [L]
RewriteRule ^([^/]+/[^/]+/[^/]+)$ /index.php?id=%1&action=$1 [L]

but when i try to access the main page for example example.com/admin or example.com/admin/categories it doesnt display anymore.

jdMorgan

10:52 pm on Dec 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should be aware that a RewriteCond only applies to the single RewriteRule that follows it.

Therefore, if you want to use "%1" in the last two rules, you need to copy the RewriteConds from the first rule into each of the second rules. Otherwise, %1 may be blank or contain an unexpected value.

Also, you need to check your script and make sure that passing "&action=admin/categories" to the script is what it expects and will work.

Jim

jdMorgan

3:47 pm on Dec 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It may be something as simple as adding an exclusion to those rules which you do not want applied to "/admin/" requests. Ahead of the "!^index\.php" exclusion, add

RewriteCond $1 !^admin/

to disable each rule when URL-paths starting with '/admin/' are requested.

Jim