简体   繁体   中英

PHP / htaccess redirect via GET

I want:

  • sub.domain.com to load index.php
  • sub.domain.com?s=custom to load /custom/index.php

I'm doing this right now:

In .htaccess:

RewriteRule ^(custom)/?$ index.php?subject=$1

and in index.php I have this:

if($_GET['s'] == 'custom') {
   header( 'Location: http://sub.domain.com/custom/index.php' ) ;
}

... but is it possible to do the redirect via htaccess itself depending on the GET variable?

Thanks!

Yes, you could check query string with RewriteCond

RewriteCond %{QUERY_STRING} s=custom
RewriteRule ^(.*?)$ custom/index.php [L]

With this, it redirects all requests with existing "custom" get parameter to index.php, and this can be extended :)

Idea: http://statichtml.com/2010/mod-rewrite-baseon-on-query-string.html

You probably want to use "RedirectMatch"

RedirectMatch ^sub.domain.com?s=custom$  /custom/index.php

Maybe youll need to play with the regex abit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM