301 Redirect of folder to parent folder - wordpress

I need to redirect /portfolio/year/ to /portfolio/.
The issue with the 301 I have written currently (redirect 301 /portfolio/year http://example.com/portfolio/) is that it redirects when someone attempts to access /portfolio/year/2000 or any of the other pages inside /year/.
Is there a way I can do this redirect? I need to keep bots and people from accessing the blank /portfolio/year/ page without breaking the rest of the internal pages.

You don't have to use PHP for this. Just replace your old RewriteRule with this one: RewriteRule ^portfolio/year/?$ portfolio [R=301] The $ at the end of the url makes sure that there are no more characters after portfolio/year/ or portfolio/year

Related

htaccess 301 redirects for some URLs of acquired company site

I want to move some content from a website of a company we just acquired (company b) to my company's website (company a) and redirect those links from b to a. Easy enough, but I want to get rid of everything else that I am not moving over to company a. There are 950 total pages and I am using 150 of those. If I set up my htaccess file with the 150 redirects for the content I want, then have the last redirect in the .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/ [R=301,L]
Would that redirect everything that I didn't set up with a redirect to the home page of my company site?
Would that redirect everything that I didn't set up with a redirect to the home page of my company site?
Yes, providing you have used mod_rewrite RewriteRule redirects for the other redirects and not mod_alias Redirect (or RedirectMatch). However, there is no need to capture the URL-path in the RewriteRule pattern, ie. change (.*) to .* or simply ^ to match everything.
You should test with 302 (temporary) redirects to avoid caching issues should you make a mistake. 301 (permanent) redirects are cached persistently by the browser - including any errors.
You've tagged your question "WordPress". All these redirects need to go before the WordPress front-controller, at the top of the config file.
I want to get rid of everything else
However, if you want to "get rid of everything else" then consider serving a 404 (or 410 Gone) instead of mass redirecting to the homepage (which will likely be seen as a "soft-404" anyway in terms of SEO). For example:
RewriteRule ^ - [R=404]
or
RewriteRule ^ - [G]
To send a 410 response. The L flag is not required in this instance.

301 Redirect server IP to domain while keeping query string

I've searched around but no one has the exact issue as me, I can't figure out how to do this without messing up the site.
-My website is WordPress.
-Vultr VPS with Server Pilot
-Currently the site is behind cloudflare and this all works.
Issue: I have an entire version of the website still browsable using the server IP and I can't figure out how to 301 redirect it.
Example if you happen to go to my website xxx.x.xx.xx then you can browse the entire website without ever being redirected, the query strings and everything shows in the address bar example:
http://xxx.x.xx.xx/category/page/?utm_source=aa&utm_medium=bb&utm_campaign=cc
Someone could browse the entire site like this, I do not want this I want to redirect it to my domain. In WordPress general settings I do have the Site URL and WordPress URL set to my domain name but I don't know why this is happening.
I can't give examples of what I've tried because they all failed and I had to delete them from my .htaccess file, basically, I want to 301 redirect any request coming to the IP and send them to the domain of the site carrying over their query string and everything, help is really appreciated.
Did you try this in your .htaccess file?
RewriteEngine On
RewriteBase /
Redirect 301 /old_page1/ http://example.com/new_page1
Redirect 301 /old_page2/ http://example.com/new_page2
Fixed it by using the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^xx\.xx\.xxx\.xxx
RewriteRule (.*) https://example.com/$1 [R=301,L]
</IfModule>
Dot's must be escaped and the query strings get carried along just fine, be sure to leave the trailing slash as it's displayed above or else your page might appear like this: https://example.compage/subpage
Leaving the trailing slash in the rewrite rule will prevent that so it would be: https://example.com/page/subpage

Why is my 301/302 stuck in a redirect loop?

this is my first question here so I hope I'm asking correctly. Apologies if I give too much or too little info.
I'm currently trying to gradually migrate my existing website www.essexmums.org to www.essexmums.com - the old site is a mix of html and wordpress but I'm moving it to a wordpress multisite. This involves moving a section at a time and creating 301 redirects to the new site.
I previously used this code to redirect www.essexmums.org/blog to www.essexmums.com/blogandnews so that every article is redirected to the same page within the new installation:
Redirect 301 /blog http://www.essexmums.com/blogandnews/
and it worked fine. I'm now trying to do the same with redirecting www.essexmums.org/recipes to www.essexmums.com/recipes but everything I do seems to get me stuck in a redirect loop. I'm trying them out with 302 redirects rather than 301 but basically I have tried both:
Redirect 301 /recipes http://www.essexmums.com/recipes/
Redirect 301 /recipes http://www.essexmums.com/recipes
and then also have tried to redirect a single page to see if that worked, such as:
Redirect 302 /recipes/tabbouleh-for-toddlers/ http://www.essexmums.com/recipes/tabbouleh-for-toddlers/
but this gets stuck too and I get a 'too many redirects' page.
What am I doing wrong? I'm sure it must be something stupid and entirely my fault so would welcome any suggestions!
Thanks
Are they both running off the same website? You can do this to avoid the loop:
RewriteEngine on
RewriteCond %{HTTP_HOST} !=www.essexmums.com
RewriteCond %{REQUEST_URI} =/recipes/
RewriteRule ^ http://www.essexmums.com/recipes/ [R=301,L]
I guess your first worked because the new URL is different.
You only need the first line once, the other three are needed for each redirect. It says if the host is not www.essexmums.com and the address is /recipes/ then redirect to http://www.essexmums.com/recipes/.

Redirect some .htm page to WordPress page without path and query string

I want to permanent redirect some .htm page from subdomain to main domain WordPress page, for this I am using this code
Redirect permanent /cat/FSBO76.htm http://www.example.com/cat/my-favorite/
But I am getting a problem normally my WordPress page working fine but when I click on old link its redirect with trailing query string and showing page not found error.
http://www.example.com/cat/my-favorite/?cat=FSBO76
Can anyone tell me how can I redirect without path and query string? or any way to solve this problem.
I already try with Redirect 301 also.
EDIT: Its only happening for .htm files, all other files and directory redirecting properly.
This sounds like maybe mod_alias (Redirect) and mod_rewrite is interferring with each other. If you already have wordpress rewrite rules in your htaccess file, you need to stick with mod_rewrite instead of using mod_alias here. Try using something like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteRule ^cat/FSBO76.htm$ http://www.example.com/cat/my-favorite/ [L,R=301]
where "subdomain.example.com" is your subdomain.

htaccess redirect wordpress folder change

I currently have a url of http://example.com/wordpres/
I'm going to change this to http://example.com/w/
I currently get a lot of traffic from links already out on the web. How can I redirect everything that went to /wordpress to /w ?
RedirectMatch 301 ^wordpress(.*)$ /directory/path/to/w/$1
The above line should match every request coming to your domain with 'wordress' at the start and redirect it to /w/. The '301' tells the browser (and search engines) that the page(s) have moved permenantly. The $1 exists to redirect anything after /wordpress/ and append it to the redirected URL. So if I visit http://example.com/wordpress/this-post/ I would get redirected to http://example.com/w/this-post.
Add this rule to your .htaccess file:
RewriteEngine on
RewriteRule wordpress(.*)$ /w$1
If doesnt work.. let me know... :)

Resources