简体   繁体   中英

How to redirect all subdomain URL to main domain using htaccess?

My old blog URL is http://www.blog.example.com/

My new blog URL is https://example.com/

I want to know how can I redirect all my old blog URLs to my new blog URLs.

For example, I want to redirect this URL:

http://www.blog.example.com/excellent-examples-forms-web-design

to

https://example.com/excellent-examples-forms-web-design

You can do this using mod_rewrite in .htaccess . mod_rewrite is required in order to check the requested hostname.

For example, at the top of your .htaccess file:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.blog\.(example\.com) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

The %1 backreference matches example.com in the preceding CondPattern (this simply saves repetition).

Test first with a 302 (temporary) redirect and change to a 301 (permanent) redirect when you are sure it's working OK - to avoid potential caching issues.

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