Fake Subdomain with mod-rewrite

自闭症网瘾萝莉.ら 提交于 2019-12-12 03:54:19

问题


Sorry to ask this... I have seen various answers on this but I can't find one to help me (prob because I am very new to this process)

Heres the issue. I want to fake a sub domain so that people go to sub.domain.co.uk but the server reads it as domain.co.uk/?event=sub then they can add other prams like sub.domain.co.uk/Login which leads to domain.co.uk/?event=sub&url=Login etc etc

The code I have so far is this:

RewriteCond %{REQUEST_URI}  !\.(php|html?|jpg|gif)$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]

# Rewrite event / url / action1 / action 2 etc.
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.co\.uk$ [NC]
RewriteRule ^/$ /index.php?event=%1 [QSA,L,NC]
RewriteRule ^([^/]*)/$ /index.php?event=%1&url=$1 [QSA,L,NC]
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?event=%1&url=$1&action1=$2 [QSA,L,NC]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/$ /index.php?event=%1&url=$1&action1=$2&action2=$3 [QSA,L,NC]

Which is almost there... its getting the url, action1 and action2 but not the event!.. if I take the / off so it looks like this RewriteRule ^$ index.php?event=%1 [QSA,L,NC] then I get the event BUT it tried to load the next command so

sub.domain.co.uk

loads

domain.co.uk/?event=sub&url=

Which is a bugger because when I load action2 it 404's on me! Someone help its driving me nuts


回答1:


Right this is a down and dirty fix I have done. my .htaccess is now like this:

# Rewrite event / url / action1 / action 2 etc.
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.co\.uk$ [NC]
RewriteRule ^/$ /index.php [QSA,L,NC]
RewriteRule ^([^/]*)/$ /index.php?url=$1 [QSA,L,NC]
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?url=$1&action1=$2 [QSA,L,NC]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/$ /index.php?url=$1&action1=$2&action2=$3 [QSA,L,NC]

and in the php I have this:

$sub = explode ( '.' , $_SERVER['HTTP_HOST'] );
$event = $sub[0] == 'www' || $sub[0] == 'domain' ? false : $sub[0] ;

Now I can see if the $event var is loaded or not...

If anyone has a cleaner way I am all ears but this is working for me HUZZAR



来源:https://stackoverflow.com/questions/15168154/fake-subdomain-with-mod-rewrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!