如何通过.htaccess将example.com:84重定向至主站首页example.com?
Redirect example.com:84 to Your Homepage via .htaccess
Hey there! Let's get that port 84 redirect sorted for you. Here's exactly how to make anyone visiting example.com:84 automatically land on your main example.com homepage using an .htaccess file:
Step 1: Confirm mod_rewrite is enabled
First, double-check that your Apache server has the mod_rewrite module turned on. Most shared hosting providers enable this by default, but if you run into issues, you can check your hosting control panel (like cPanel) or reach out to your host's support team to confirm.
Step 2: Add the rewrite rules to .htaccess
Create or edit the .htaccess file in your website's root directory (where your main index.html/index.php file lives) and add these lines:
RewriteEngine On # Redirect port 84 traffic to the main domain RewriteCond %{SERVER_PORT} ^84$ RewriteRule ^(.*)$ https://example.com/ [R=301,L]
Let's break down what each line does:
RewriteEngine On: This enables Apache's URL rewriting functionality—without this, none of the rules below will work.RewriteCond %{SERVER_PORT} ^84$: This condition checks if the incoming request is using port 84. The^and$ensure we're matching exactly port 84, not just any port that includes the number 84.RewriteRule ^(.*)$ https://example.com/ [R=301,L]:^(.*)$captures any path appended toexample.com:84(like/old-page). If you want to preserve that path on your main domain (e.g., sendexample.com:84/old-pagetoexample.com/old-page), keep the$1at the end:https://example.com/$1. If you want everyone to land strictly on the homepage, leave it ashttps://example.com/.R=301: This marks the redirect as permanent, which is best for SEO (it tells search engines to update their indexes to the new URL). If you're testing and want a temporary redirect first, useR=302instead, then switch to 301 once you confirm it works.L: This stands for "last"—it tells Apache to stop processing any further rewrite rules once this one matches, preventing conflicts with other rules you might have.
Quick Notes:
- Replace
https://example.comwith your actual domain (usehttp://if you don't have an SSL certificate, but HTTPS is always recommended for security). - If you already have existing rules in your
.htaccess, add these lines near the top—before any other rewrite rules that might take precedence. - Test it out by typing
example.com:84into your browser—you should be instantly redirected to your main homepage.
内容的提问来源于stack exchange,提问作者John Carter
相关产品推荐
相关产品推荐

