【问题标题】:.htaccess conditional statement.htaccess 条件语句
【发布时间】:2013-03-04 12:09:32
【问题描述】:

我想使用通配符子域将我的 url 重写为更多 seo url,我无法编写 htaccess 条件并希望得到一些帮助。

我有这个网址:

http://learntipsandtricks.com/blog/c/magento

我想改写为:

http://magento.learntipsandtricks.com/

更改分页链接:

http://learntipsandtricks.com/blog/92/magento/3

到:

http://magento.learntipsandtricks.com/p-92-3

最后更改文章网址:

http://learntipsandtricks.com/blog/magento/114/magento-index-management-Cannot-initialize-the-indexer-process

到:

http://magento.learntipsandtricks.com/114-magento-index-management-Cannot-initialize-the-indexer-process

这是我写的:

RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} (.+)\.learntipsandtricks\.com\/p\-(\d+)\-(d+) [NC]
RewriteRule ^(.*)$ http://learntipsandtricks.com/blog/%2/%1/%3/$1 [P]

RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} (.+)\.learntipsandtricks\.com\/(\d+)\-(.*) [NC]
RewriteRule ^(.*)$ http://learntipsandtricks.com/blog/%1/%2/%3/$1 [P]

RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} (.+)\.learntipsandtricks\.com [NC]
RewriteRule ^(.*)$ http://learntipsandtricks.com/blog/c/%1/$1 [P]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php/$1 [L]

上面的代码不起作用。是否可以在 htaccess 中使用 if else 来检查所有类型的 url 并相应地重写?

编辑:

RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} (.+)\.learntipsandtricks\.com [NC]
RewriteCond %{REQUEST_URI} ^/p-(\d+)-(\d+)$ [NC]
RewriteRule ^(.*)$ http://learntipsandtricks.com/blog/%1/[first part of sub-domain]/%2/$1 [P]

如何在此处获取子域的第一部分。

谢谢。

【问题讨论】:

    标签: .htaccess seo subdomain wildcard-subdomain


    【解决方案1】:

    HTTP_HOST 不包含 URL 路径。如果要匹配 URL 路径,请改用 REQUEST_URI

    RewriteCond %{REQUEST_URI} ^/p-(\d+)-(\d+)$
    

    例如。

    来自RewriteCond Directive

    • RewriteCond 反向引用:这些是 %N (0 从当前条件集中的最后一个匹配的 RewriteCond。 %0 提供对该模式匹配的整个字符串的访问。

    因此,如果要同时捕获域和 URL 路径组件,则必须将 HTTP_HOSTREQUEST_URI 合并为一个 RewriteCond

    RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
    RewriteCond %{HTTP_HOST} (.+)\.learntipsandtricks\.com [NC]
    RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^(.+)\.learntipsandtricks\.com/p-(\d+)-(\d+)$ [NC]
    RewriteRule ^(.*)$ http://learntipsandtricks.com/blog/%2/%1/%3/$1 [P]
    

    【讨论】:

    • 谢谢。你知道如何获取子域的第一部分,请参阅上面的编辑。
    • 太棒了!谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 2016-06-25
    • 2013-03-09
    相关资源
    最近更新 更多