最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

htaccess wildcard redirect misses some URLs

programmeradmin3浏览0评论

I am using a wildcard redirect to redirect my old domain to a new domain. It works great but misses a few URLs (they don't get redirected to the destination and return status 200)

Can you please help me understand what's wrong with this? Maybe I am doing the wildcard redirect incorrectly or it's a server issue. My old domain is old.example and my new domain is new.example.

Example URL which isn't getting redirected: old.example/example-url

Here's my .htaccess file:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

#Options +FollowSymLinks

# Redirect everything
RewriteRule ^(.*)$ /$1 [R=301,L]

I am using a wildcard redirect to redirect my old domain to a new domain. It works great but misses a few URLs (they don't get redirected to the destination and return status 200)

Can you please help me understand what's wrong with this? Maybe I am doing the wildcard redirect incorrectly or it's a server issue. My old domain is old.example and my new domain is new.example.

Example URL which isn't getting redirected: old.example/example-url

Here's my .htaccess file:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

#Options +FollowSymLinks

# Redirect everything
RewriteRule ^(.*)$ https://new.example/$1 [R=301,L]
Share Improve this question edited Apr 24, 2020 at 18:35 Naser Mohd Baig asked Apr 24, 2020 at 17:44 Naser Mohd BaigNaser Mohd Baig 256 bronze badges 2
  • Is you new domain on a different server? – MrWhite Commented Apr 24, 2020 at 17:52
  • No @MrWhite, it's on the same server. Will that cause any issue? – Naser Mohd Baig Commented Apr 24, 2020 at 18:01
Add a comment  | 

1 Answer 1

Reset to default 1

You've put the redirect directive in the wrong place. It needs to go before the WordPress front-controller, otherwise, the redirect will simply get ignored for anything other than URLs that map directly to the filesystem.

Since these domains are also on the same server (same hosting account I assume) then you will need to check for the requested hostname, otherwise, you'll get a redirect loop.

For example:

# Redirect everything from old to new domain
RewriteCond %{HTTP_HOST} ^old\.example [NC]
RewriteRule (.*) https://new.example/$1 [R=301,L]

# BEGIN WordPress
# :

The regex ^(.*)$ can be simplified to (.*) since regex is greedy by default.

发布评论

评论列表(0)

  1. 暂无评论