仅当请求uri为空时才重写htaccess

时间:2016-10-28 00:19:26

标签: php regex apache .htaccess url-rewriting

我目前的.htaccess如下:

##FOR DUAL LEVEL TIER SUBDOMAIN
#SUBDOMAIN REWRITE for COUNTRY.CATEGORY.uqloo.com
RewriteCond %{HTTP_HOST} !www.mydomain.com$ [NC] # Presuming you don't want to do www
RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.mydomain\.com [NC] # Catch subdomain
RewriteCond %{REQUEST_URI} !\.(?:jpe?g|png|gif|bmp|php)$ [NC]
RewriteRule ^(.*)$ index.php?sub1=%1&sub2=%2 [QSA,L]

##FOR SINGLE LEVEL TIER SUBDOMAIN
RewriteCond %{HTTP_HOST} !www.mydomain.com$ [NC] # Presuming you don't want to do www
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com [NC] # Catch subdomain
RewriteCond %{REQUEST_URI} !\.(?:jpe?g|png|gif|bmp|php)$ [NC]
RewriteCond %{REQUEST_URI} !^cat/([A-Za-z0-9-]+)?$ [NC] # Don't rewrite if we already have
RewriteRule ^(.*)$ index.php?sub1=%1 [QSA,L]

我尝试进行单级和双级子域重定向,同时保留URI部分的所有其他重写相关。我想保持子域的参数与URI重写相关。

以下规则将继续将 index.php 重定向回 pages/details.php ,参数sub1和sub2可用如果子域存在。

RewriteRule ^([0-9-]+)?/([A-Za-z0-9-]+)?$

pages/details.php?adid=$1&alias=$2 [NC,L]

1 个答案:

答案 0 :(得分:0)

您可以使用以下规则:

RewriteEngine On

# pages URL handlers
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^([0-9-]+)/([a-z0-9-]+)/?$ pages/details.php?sub1=%1&adid=$1&alias=$2 [NC,L,QSA]

RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^([0-9-]+)/([a-z0-9-]+)/?$ pages/details.php?sub1=%1&sub2=%2&adid=$1&alias=$2 [NC,L,QSA]

# subdomain rewrites
RewriteCond %{REQUEST_URI} !\.(?:jpe?g|png|gif|bmp|php)$ [NC]
RewriteCond %{REQUEST_URI} !^/cat/([A-Za-z0-9-]+)?$ [NC]
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^ index.php?sub1=%1 [QSA,L]

RewriteCond %{REQUEST_URI} !\.(?:jpe?g|png|gif|bmp|php)$ [NC]
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^ index.php?sub1=%1&sub2=%2 [QSA,L]