WordPress固定链接URL重定向及自建站点配置与域名变更问题咨询
Hey there! Let's break down your two questions step by step—they're both super common when migrating from WordPress.com to a self-hosted setup, so I’ve got you covered.
First, let’s cover the most likely scenario: you’re setting up a self-hosted WordPress site (since you mentioned a PHP environment, which WordPress runs on). If you’re building a custom PHP site instead, I’ve included that too.
If You’re Using Self-Hosted WordPress
- Log into your WordPress admin dashboard.
- Navigate to Settings → Permalinks.
- WordPress.com’s default permalink structure is the Day and name option, which looks like
https://yourdomain.com/2024/05/20/your-post-title/. Just select that radio button and click Save Changes. - If you used a custom structure on WordPress.com (like just the post name:
https://yourdomain.com/your-post-title/), choose the Post name option or paste your exact custom structure into the Custom Structure field (e.g.,/%postname%/). - Pro tip: WordPress will usually update your
.htaccessfile automatically. If it can’t, you’ll see a code snippet on the Permalinks page—copy that and paste it into your site’s root.htaccessfile to make the permalinks work.
If You’re Building a Custom PHP Site
If you’re not using WordPress, you’ll need to set up URL rewriting via your server config. For Apache (the most common server for PHP sites), edit or create a .htaccess file in your site’s root:
- To mimic WordPress.com’s Day and name structure (
/year/month/day/post-name/), add these rules:
RewriteEngine On RewriteBase / RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/([^/]+)/?$ index.php?year=$1&month=$2&day=$3&post=$4 [L,QSA]
- To mimic the Post name structure (
/post-name/), use:
RewriteEngine On RewriteBase / RewriteRule ^([^/]+)/?$ index.php?post=$1 [L,QSA]
- You’ll then need to update your PHP code to read the query parameters (
year,month,day,post) and load the correct content.
Yes, you can absolutely redirect exact URLs like www.olddomain.com/aze/123 to www.newdomain.com/aze/123. Permanent 301 redirects are best here—they preserve your SEO and tell browsers/search engines the content has moved permanently.
Option 1: Apache Server (Using .htaccess)
Add these lines to your old site’s .htaccess file (or the new site’s if you’ve already migrated the content):
RewriteEngine On RewriteCond %{HTTP_HOST} ^www.olddomain.com$ [NC] RewriteRule ^(.*)$ https://www.newdomain.com/$1 [R=301,L]
- The
(.*)captures everything after the old domain, and$1inserts that exact path into the new URL. If you don’t usewww, adjust theRewriteCondto^olddomain.com$and the new URL tohttps://newdomain.com/$1.
Option 2: Nginx Server
If your server runs Nginx, add this to your server block for the old domain:
server { listen 80; listen [::]:80; server_name www.olddomain.com olddomain.com; return 301 https://www.newdomain.com$request_uri; }
- The
$request_urivariable grabs the full path and query string, so every exact URL gets redirected perfectly.
Option 3: WordPress Plugin (For Self-Hosted WP)
If you’re using self-hosted WordPress, you can use a plugin like Redirection to set up precise redirects. This is handy if you need to handle individual URL exceptions, but server-level redirects (above) are more efficient for full domain migrations.
内容的提问来源于stack exchange,提问作者user8633372

