【问题标题】:Redirecting the user requests with apache mod-rewrite使用 apache mod-rewrite 重定向用户请求
【发布时间】:2012-06-26 19:22:50
【问题描述】:

我有以下重写规则将所有内容重定向到 index.php

RewriteEngine On
RewriteCond %{REQUEST_URI} (/[^.]*|\.)$ [NC]
RewriteRule .* index.php [L]

现在我需要对其进行修改以支持以下内容 当用户尝试访问 domain.tld 时,将他重定向到 www.domain.tld 当用户尝试访问 domain.tld/key/... 时将他(永久重定向)重定向到 key.domain.tld/...

当然,我仍然需要将所有流量重定向到 index.php,除非用户尝试访问我需要直接提供服务的 css、javascript 和图像

domain.tld/css/... subdomain.domain.tld/css/... apache 应该直接提供 url(css 文件) 同样我需要添加 js 和 images 文件夹

我对 Apache ReWrite 模块感到很糟糕,所以请在这里帮助我 :)

【问题讨论】:

    标签: apache mod-rewrite


    【解决方案1】:
    <IfModule mod_rewrite.c>
      RewriteEngine on
    
      RewriteCond %{REQUEST_FILENAME} !-f #if a file, css, js, jpg whatever file
      RewriteCond %{REQUEST_FILENAME} !-d #if thats a directory
    
    RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
    RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
    RewriteRule ^$ /someprofile.php?username=%2 [QSA,L]
    
    </IfModule>
    

    【讨论】:

    • 这给了我一个 500 服务器错误,除了 www.domain.tld :( 我搜索了所有日志并尝试了RewriteLog "/usr/local/var/apache/logs/rewrite.log",但它似乎没有产生任何东西。有什么想法吗?
    • 现在试试,我更新了它..你想把用户从 domain.com/username 移动到 username.domain.com 吗?真的没有得到你。你想做什么?
    • 所以,一次一件事,我想 1. 当没有子域时添加 www 子域 domain.com/..anything.. =&gt; www.domain.com/..anything.. 2. 当存在并且是第一个组件时获取特定的单词“key”并添加它作为子域并在域domain.com/key/..anything.. =&gt; key.domain.com/..anything.. 之后扩展其他所有内容,并将其标记为永久重定向。
    猜你喜欢
    • 2011-05-06
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    相关资源
    最近更新 更多