简体   繁体   中英

htaccess rules to rewrite subfolder to a GET variable

I'm not very experienced with htaccess. today I fail in writing an .htaccess to rewrite a url.

What I want: My project is hosted on a subdomain eg sub.domain.com

My htaccess should manage:

  1. sub.domain.com should redirect to www.domain.com (Redirect 301, that's clear)
  2. subfolders should be put as GET variable eg sub.domain.com/test to www.domain.com?var=test
  3. the subfolder /admin shouldn't be redirected

Thanks in advance for tips!

This should do it for you:

RewriteEngine on
RewriteCond %{HTTP_HOST} =sub.example.com
# Following condition added to support changed requirement, see comments
RewriteCond %{QUERY_STRING} !(?:^|&)var=
RewriteCond %{REQUEST_URI} !^/admin/
RewriteRule ^(.*)$ http://www.example.com/?var=$1 [R=301,L]

This will drop any query string that was in the original request. If you want to keep any query string, and just append 'var=...' to it, then add QSA to the flags at the end of the last line, separated by a comma.

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