【问题标题】:I want to rewrite subdomains and folders to params我想将子域和文件夹重写为参数
【发布时间】:2015-02-22 18:57:22
【问题描述】:

我喜欢重写我的子域站点(通配符)以及何时将文件夹改写为 index.php 文件的参数。

所以

www.domain.com
www.domain.com/js/file.js
www.domain.com/css/file.css
www.domain.com/images/file.jpg

应该保留!

这些应该更改为 index.php 的参数

sub.domain.com/

www.domain.com/index.php?key1=sub

sub.domain.com/2015

www.domain.com/index.php?key1=sub&key2=2015

sub.domain.com/2015/12

www.domain.com/index.php?key1=sub&key2=2015&key3=12

这可能吗?

到目前为止我所拥有的是

RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC] RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com [NC] RewriteRule ^/?(.*)$ $1?page=%1 [QSA,L,NE]


非常感谢!

所以我重写了一些@anubhava 他的 htaccess

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.gebruikmaar\.nl [NC]
RewriteRule ^/?(.*)$ $1?page=%1 [QSA,L]

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.gebruikmaar\.nl [NC]
RewriteRule ^(.*)([^/][0-9]*)(/?)$ $1?page=%1&key2=$1 [QSA,L]

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.gebruikmaar\.nl [NC]
RewriteRule ^(.*)([^/][0-9]*)/([^/][0-9]*)(/?)$ $1?page=%1&key2=$1&key3=$2 [QSA,L]

它几乎可以工作了!

这个网址:
http://agenda.gebruikmaar.nl
变成
http://www.gebruikmaar.nl/index.php?page=agenda


但是 :-(
这个网址:
http://agenda.gebruikmaar.nl/2015
变为:
http://www.gebruikmaar.nl/index.php?page=agenda&key1=201


那么最后一个字符(5)在哪里?

【问题讨论】:

    标签: .htaccess subdomain subdirectory


    【解决方案1】:

    您可以在 DOCUMENT_ROOT/.htaccess 文件中使用此代码:

    RewriteEngine On
    RewriteBase /
    
    # skip all files and directories from rewrite rules below
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]
    
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com [NC]
    RewriteRule ^/?$ index.php?key1=%1 [QSA,L]
    
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com [NC]
    RewriteRule ^([^/]+)/?$ index.php?key1=%1&key2=$1 [QSA,L]
    
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com [NC]
    RewriteRule ^([^/]+)/([^/]+)/?$ index.php?key1=%1&key2=$1&key3=$2 [QSA,L]
    

    【讨论】:

    • 很抱歉,我没有得到任何 GET 回复。
    • 它在文档根目录中,是的。并且没有其他规则。我要去的网址是 -> agenda.gebruikmaar.nl/2015 (还没有上线,它在我的流浪环境中)并且应该将 2 个参数:议程和 2015 发送到 index.php 文件。
    • 当使用您的精确代码时,它会显示:在此服务器上找不到请求的 URL /2015/。 :-)
    • 然后转到agenda.gebruikmaar.nl 它不会将议程参数传递给 index.php($_REQUEST 为空)
    • (是的,将 domain\.com 更改为 gebruikmaar\.nl :-))
    猜你喜欢
    • 2023-03-27
    • 2019-12-21
    • 2010-12-28
    • 1970-01-01
    • 2019-05-19
    • 2017-07-04
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多