Forum Moderators: phranque
#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
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
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]
how can i make this redirect
http://example.com/register -> index.php?action=register
what line do i have to add?
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]
# 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.
# 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
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
[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.
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?
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.
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