简体   繁体   English

htaccess规则将子文件夹重写为GET变量

[英]htaccess rules to rewrite subfolder to a GET variable

I'm not very experienced with htaccess. 我对htaccess不是很有经验。 today I fail in writing an .htaccess to rewrite a url. 今天我写了一个.htaccess来重写一个url失败了。

What I want: My project is hosted on a subdomain eg sub.domain.com 我想要的:我的项目托管在子域,例如sub.domain.com

My htaccess should manage: 我的htaccess应该管理:

  1. sub.domain.com should redirect to www.domain.com (Redirect 301, that's clear) sub.domain.com应该重定向到www.domain.com (重定向301,很清楚)
  2. subfolders should be put as GET variable eg sub.domain.com/test to www.domain.com?var=test 子文件夹应作为GET变量,例如sub.domain.com/testwww.domain.com?var=test
  3. the subfolder /admin shouldn't be redirected 子文件夹/admin不应重定向

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. 如果要保留任何查询字符串,只需在其中附加'var = ...',则将QSA添加到最后一行末尾的标志中,用逗号分隔。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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