首页 文章

子域上GET参数的Mod重写

提问于
浏览
6

我正在尝试将子域(非REDIRECT)重写为 $_GET 参数,如下所示:

Desired result:

http://go.example.bz/link/abcde   ->   http://example.bz/go/link?id=abcde
or
http://go.example.bz/hrm/employee/8   ->   http://example.bz/go/hrm/employee?id=8

目前有什么工作:

http://example.bz/go/link/abcde -> http://example.bz/go/link?id=abcde
and
http://example.bz/go/hrm/employee/8 -> http://example.bz/go/hrm/employee?id=8

使用root中的.htaccess:

RewriteEngine On

RewriteRule ^go/link.php/([^/\.]+)/?$ go/link.php?id=$1 [L]
RewriteRule ^go/hrm/employee.php/([^/\.]+)/?$ go/hrm/employee.php?parameter=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

AddCharset UTF-8 .php
Options -Indexes

这就是我重定向子域的方式:

<VirtualHost *:80> 
   Servername go.example.bz DocumentRoot /var/www/go
</VirtualHost>

I do not want to redirect 到 - >目的地,而不是保留http://go.example.bz/link/abcde网址但是有/ link的结果?abcde

4 回答

  • 0

    我想我的问题来自于不从子域URL中删除.php扩展名,我在这里找到了答案https://css-tricks.com/forums/topic/remove-php-extension-from-subdomain-urls/然后我交替使用我的RewriteRules:

    RewriteEngine On
    
    RewriteRule ^go/link/([^/\.]+)/?$ go/link.php?id=$1 [L]
    RewriteRule ^go/hrm/employee/([^/\.]+)/?$ go/hrm/employee.php?id=$1 [L]
    
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^go\.example\.bz$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.go\.example\.bz$
    RewriteRule ^/?$ "http\:\/\/tgc\.bz\/go" [R=301,L]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^([^\.]+)/$ $1.php
    
    AddCharset UTF-8 .php
    Options -Indexes
    
  • 1

    您可以在root .htaccess中使用此规则:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.(example\.com)$ [NC]
    RewriteRule ^(link)/(.+)$ http://%2/%1/$1?id=$2 [NC,L,QSA,R=302]
    
  • 2
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^go.example.com
    RewriteRule ^(.*?)/(.*)$ http://example.com/go/$1?id=$2 [L]
    
  • 0

    这有用吗:

    RewriteCond %{HTTP_HOST} ^go.example.com$
    RewriteRule ^link/([a-zA-Z0-9]+)$ http://example.com/go/link?id=$1 [P]
    

相关问题