.htaccess rewrite wildcard subsubdomain and params

本小妞迷上赌 提交于 2019-12-24 06:42:05

问题


I'm trying to write a mod_rewrite to work like this

  • wildcard.subdomain.domain.com => subdomain.domain.com/data.php?q=wildcard
  • wildcard.subdomain.domain.com/?repeat=1 => subdomain.domain.com/data.php?q=wildcard&repeat=1
  • subdomain.domain.com => data.php
  • www.subdomain.domain.com => data.php

What I've done so far in my .htaccess

<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^([^.]+)\.abc\.def\.com\.br$ [NC]
  RewriteCond %1 !^(www)$ [NC]
  RewriteRule ^.*$ data.php?q=%1 
</IfModule>`

回答1:


Try these:

  # This should cover the first 2 cases
  RewriteCond %{HTTP_HOST} ^([^.]+)\.abc\.def\.com\.br$ [NC]
  RewriteCond %1 !^(www)$ [NC]
  RewriteRule ^$ /data.php?q=%1&%{QUERY_STRING} [L,QSA]

  # This should cover the last 2
  RewriteCond %{HTTP_HOST} ^([^.]+)\.abc\.def\.com\.br$ [NC]
  RewriteCond %1 ^(www)$ [NC]
  RewriteRule ^$ /data.php?%{QUERY_STRING} [L,QSA]

Note that this does nothing in the cases where the request is: wildcard.subdomain.domain.com/some/path/to/file.html These only match request URI's that are /.




回答2:


For repeat parameter use

RewriteRule ^.*$ data.php?q=%1&%{QUERY_STRING}



回答3:


if you are using a cpanel , you can create a * sub domain (*.domain.com) and connect it to a directory , then create a index.php in that directory :

<?php
echo "<pre>";
print_r($_SERVER);
echo "</pre>";
?>

test the result , you can export sub domain names from server name or server host



来源:https://stackoverflow.com/questions/11568569/htaccess-rewrite-wildcard-subsubdomain-and-params

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