简体   繁体   English

如何使用htaccess重定向不符合条件的所有其他URL

[英]How to 301 redirect all other URLs that do not meet conditions using htaccess

I am doing bunch of 301 redirects via the htaccess file for a WordPress website. 我正在通过htaccess文件为WordPress网站进行一堆301重定向。 I have listed about 80 specific URLs/directories that point to their new relative position on a new domain. 我列出了大约80个特定的URL /目录,这些URL /目录指向它们在新域上的新相对位置。

However, there are still a bunch of URLs that are lurking around, that I want to redirect to the new domains home page. 但是,仍然有很多URL潜伏着,我想重定向到新的域主页。

What I am trying to do is if the URL does not meet any of the hardcoded URLs, redirect it to the home page. 我想做的是,如果URL不符合任何硬编码的URL,请将其重定向到主页。

Here is what I have: 这是我所拥有的:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Begin Redirects
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^2015/12/14/some\-directory/$ https://www.newdomain.com/? [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^2015/14/14/someother\-directory/$ https://www.newdomain.com/? [L,R=301]
RewriteCond %{HTTP_HOST} ^olddomain\.com$
RewriteRule ^(.*) https://www.newdomain.com/? [L,R=301]
# End Redirects
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

The code that I am using to catch all other URLs and send to the home page is written as: 我用来捕获所有其他URL并发送到主页的代码写为:

RewriteCond %{HTTP_HOST} ^olddomain\.com$
RewriteRule ^(.*) https://www.newdomain.com/? [L,R=301]

The issue I am having is that it seems to jump straight to that condition, redirecting all the URLs to the home page, whether that piece of code is at the top of the htaccess file or the bottom. 我遇到的问题是,无论该代码段位于htaccess文件的顶部还是底部,它似乎都直接跳转到该状态,将所有URL重定向到主页。 Is there a way to set this as a condition so it only redirects the URL if it does not meet any of the URLs or directories above it? 是否可以将其设置为条件,以便仅在不符合其上任何URL或目录的情况下才重定向URL?

If I remember correctly, RewriteRule can't be used like that, which normally we'll write it like: 如果我没记错的话,就不能像这样使用RewriteRule ,通常我们会这样写:

RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteCond %{REQUEST_URI} ^/2015/12/14/some-directory/
RewriteRule (.*) https://www.newdomain.com/? [L,R=301]

Where your code is: 您的代码在哪里:

RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^2015/12/14/some\-directory/$ https://www.newdomain.com/? [L,R=301]

Also, you could test your .htaccess with this tool first which can save your time from transferring file between server and clearing cache (if you need). 另外,您可以首先使用此工具测试.htaccess这样可以节省从服务器之间传输文件到清除缓存的时间(如果需要)。

Second, I don't really see the meaning of your code, since all URLs from www.olddomain.com will be redirected to https://www.newdomain.com/? 其次,由于您来自www.olddomain.com所有URL都将重定向到https://www.newdomain.com/? ,因此我看不出您的代码的含义https://www.newdomain.com/? . Unless you meant, redirect base on the path but not the domain, which you'll need to remove the first line of my code. 除非您是故意的,否则请根据路径而非域进行重定向,这将需要删除代码的第一行。

Last, notice I didn't add $ behind the path, since I'm not really sure if you need a wildcard redirect which /2015/12/14/some-directory/something also redirects to https://www.newdomain.com/? 最后,请注意,我没有在路径后添加$ ,因为我不确定是否需要通配符重定向, /2015/12/14/some-directory/something也可以重定向到https://www.newdomain.com/? . If you don't, remember to add it back. 如果不这样做,请记住将其重新添加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 htaccess 永久(301)将所有 url 重定向到主页 - Permanently (301) redirect all urls to homepage with htaccess 301使用htaccess重定向博客页面和所有其他页面 - 301 redirect using htaccess for blog pages and all other pages 使用.htaccess中的301将http URL重定向到https - redirect http urls to https with 301 in .htaccess 如何使用.htaccess从子域到根进行正确的301重定向 - How do I do a proper 301 redirect from subdomain to root using .htaccess 更改自定义帖子类型后,如何使用.htaccess进行301重定向 - how to do 301 redirect using .htaccess, after changing custom post type slug 如何在htaccess中进行301重定向,同时保留原始页面的slug - How to do a 301 redirect in htaccess while keeping the slug of the original page 如何使用.htaccess进行此工作-将主域重定向到子域并将所有其他页面重定向到新域 - How do I make this work using .htaccess - redirect main domain to subdomain & redirect all other pages to new domain 重定向301我在htaccess WordPress上的所有图像 - Redirect 301 all my images on htaccess Wordpress 批处理301使用htaccess从动态URL重定向到静态页面 - batch 301 Redirect from Dynamic URLs to Static Pages with htaccess 如何使用.htaccess规则将所有传入的HTTP URL重定向到WordPress中的https - How to Redirect all incoming http urls to https in WordPress using .htaccess rule
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM