Fake Subdomain with mod-rewrite

2019-09-17 15:59发布

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条回答
干净又极端
2楼-- · 2019-09-17 16:26

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

查看更多
登录 后发表回答