Redirect Subdirectory to root domain - wordpress

I want to redirect an entire subdirectory on my site i.e. http://www.mydomain.com/subdirectory to the root domain of my site http://www.mydomain.com can someone please tell my how to do so via .htaccess?
I had tried with Cpanel and enabled wildcard redirect but, sadly it's not working!
The subdirectory is actually a separate WordPress installation.
Thanks

Look into this: 301 redirection

Put the following .htaccess file into the root directory:
Redirect /subdirectory http://www.example.com
(requires mod_alias to be loaded) or use mod_redirect and put the .htaccess file into the subdirectory:
RewriteEngine On
RewriteRule (.*) http://www.example.com/$1
See: http://httpd.apache.org/docs/current/rewrite/ for mod_rewrite documentation.

Related

Why I can't redirect subdomain from htaccess while having Cloudflare?

Why I can't redirect subdomain from htaccess while using CloudFlare SSL?
PS I don't know if I can paste URL here so I won't paste it to avoid removal of the question.
I'm getting this problem in Chrome (but it doesn't work in Firefox either)
error
I know there are fixes on the internet for this error but none worked for me. Is it because I have root domain connected to CloudFlare (also affects subdomains)?
What I did is:
1. created a subdomain in hosting and set its file directory.
2. in that file directory made .htaccess file.
3. inside the file
Redirect 301 / https://rootdomain/firstfolder
but it falls into redirects loop :(
With my .htaccess code, I check if the domain has the www. dub on the domain, then I redirect it to the subdomain without the www. dub on it. Also, because I don't think you want to change it, I place a 301 Redirect on it also.
I forgot to mention, usually people do not use the www. on the subdomain also, so I am guessing that you don't want people to use it either.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^/?$ "http\:\/\/sundomain\.domain\.com\/" [R=301,L]
For specific url : put below code in your subdomain.domain.com's .htacess
Redirect 301 /blue-car/ http://www.example.com/cars/red-car

Wordpress htaccess 301 redirect issue

could anyone help me please with the code that I need to insert in my blog's .htaccess to redirect everything from:
https://www.example/blog/wp-json/WHATEVERcomesHERE
to:
https://www.example.com/blog/
The .htaccess file resides in https://www.example/blog/ (since example.com is another story, and WP is installed on /blog/). Thanks!
This should do what you need. If you want to pass the value of "WHATEVERcomesHERE" in the redirect you can do so using $1 in the URL you want to redirect to (i.e. https://www.example.com/blog/$1).
RewriteEngine on
RewriteRule ^blog/wp-json/(.+)$ https://www.example.com/blog/ [R=301,L]
Also, the current rule would redirect /blog/wp-json/sada but not /blog/wp-json/. If you want it to redirect when there isn't anything after wp-json then change (.+) to (.*)
Here is a simple redirect, You can add this code at the end of your .htaccess file
RewriteEngine On
RewriteRule ^blog/wp-json/(.+)$ https://www.example.com/blog/ [R=301,L]
Please keep in mind that a small mistake in your .htaccess code can make your WordPress site inaccessible, and it may start showing Internal Server Error.
That’s why it is important that you backup your .htaccess file before making any changes.
Another way (Recomended)
Create Redirects in WordPress using Plugins
You can use Redirection plugin.
Install and activate the plugin. Once activated, visit Tools » Redirection to setup your redirects.
Also Simple 301 Redirects , it makes 301 Redirects simple. Simply install and activate the plugin and then visit Settings » 301 Redirects to add your URLs.

Simple .htaccess rule to redirect subfolder

How do i edit the .htaccess file to redirect all links in a subdirectory to another subdirectory
inactive url
http://example.com/dir/archive/AnythingHere
redirect location
http://example.com/dir/new/
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^dir/archive/(.*)$ /dir/new/$1 [L,NC,NE,R=302]
Make sure to place this rule as very first rule in your WP .htaccess file

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... :)

.htaccess Subfolder Redirect

I recently moved my WordPress website to a subfolder. I want to add a redirect in the .htaccess file so that the links to images I've uploaded originally to ~/wp-content/uploads/ will pass to ~/blog/wp-content/uploads. The .htaccess file must remain in the root folder for WordPress to read it properly. This is what I tried
Redirect /wp-content/uploads/ /blog/wp-content/uploads
That worked great, except I host other domains on this account, and all of the other domain's upload folders were being redirected in a similar manner.
Is there a way to restrict this redirect to just one domain? So that example.com/wp-content/uploads redirects to example.com/blog/wp-content/uploads, but another.com/wp-content/uploads does not?
Thanks everyone!
Assuming you want a 301 redirect, using this RewriteEngine example should work:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
RewriteRule ^wp-content/uploads/(.*)$ http://www.example.com/blog/wp-content/uploads/$1 [R=301,L]
You should place the Redirect inside the <VirtualHost> definition for example.com in the httpd.conf (or equivalent) instead of .htaccess
on a sidenote, Redirectsays temporary / 302 by default, so it is nicer to use
Redirect permanent /wp-content/uploads/ /blog/wp-content/uploads instead

Resources