如何将Bootstrap+PHP+MySQL双语网站默认跳转至/in目录
Hey there! Let's get your default domain redirect sorted so visitors land straight on your Indonesian language site. Below are three solid methods depending on your server setup:
This is the go-to for most shared hosting environments. Just create or edit the .htaccess file in your website's root directory and add these rules:
RewriteEngine On # Prevent redirect loops by skipping requests already in en/in folders RewriteCond %{REQUEST_URI} !^/(en|in)/ # Permanently redirect all root requests to /in RewriteRule ^(.*)$ /in/$1 [R=301,L]
The 301 status code tells search engines this is a permanent redirect, which helps with SEO. And the condition ensures visitors who manually go to /en won't get bounced back.
If you're running Nginx, head to your site's config file (usually located in /etc/nginx/sites-available/) and add this block inside your server section:
location / { if ($request_uri !~ ^/(en|in)/) { return 301 /in$request_uri; } }
After saving the changes, restart Nginx to apply them:
sudo systemctl restart nginx
If you don't have access to server config files or prefer a PHP-based solution, create an index.php file in your root directory (or edit the existing one) with this code at the very top:
<?php // Check if we're already in en or in directory $currentPath = $_SERVER['REQUEST_URI']; if (!preg_match('/^\/(en|in)\//', $currentPath)) { // Send permanent redirect to Indonesian site header("HTTP/1.1 301 Moved Permanently"); header("Location: /in" . $currentPath); exit; } ?>
This will kick in before any other Bootstrap/PHP code runs, ensuring visitors get redirected right away.
Quick Notes:
- Always test after setting up: Visit your root domain,
/en, and/into make sure there are no infinite redirect loops. - The
301redirect is important for SEO—it signals to search engines that/inis the default version of your site. - Your language switcher buttons will still work as expected since we're excluding
/enand/infrom the redirect rules.
内容的提问来源于stack exchange,提问作者Raden Octavian

