Stefan Müller
Stefan Müller

Reputation: 33

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 e.g. 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 e.g. sub.domain.com/test to www.domain.com?var=test
  3. the subfolder /admin shouldn't be redirected

Thanks in advance for tips!

Upvotes: 3

Views: 322

Answers (1)

user2493235
user2493235

Reputation:

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.

Upvotes: 2

Related Questions