【问题标题】:htaccess rewrite only if request uri is emptyhtaccess 仅在请求 uri 为空时重写
【发布时间】:2017-03-10 19:39:07
【问题描述】:

我当前的 .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]

【问题讨论】:

    标签: php regex apache .htaccess url-rewriting


    【解决方案1】:

    您可以使用这些规则:

    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]
    

    【讨论】:

    • 好的,现在可以了。但是有没有可能我在#pages URL下的重写规则也会继承sub1的参数呢?
    • 太棒了。最后一个问题。说如果我有一个 2 层域?例如: http://[country].[category].mydomain.com 。它会是什么样子?
    • 我正在寻找类似 RewriteRule ^(.*)$ index.php?sub1=%1&sub2=%2 [QSA,L];这将产生:2 $_GET 结果
    • 我们在页面内进行过滤。
    • 确定检查更新的规则。只是对未来的建议,最好在问题中预先指定所有要求。
    猜你喜欢
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    相关资源
    最近更新 更多