htaccess redirect all links except base url - wordpress

I want to redirect all links from my site except the home url to subdirectory.
Example:
www.example.com should remain www.example.com
www.example.com/someurl should redirect to www.example.com/sub/someurl
www.example.com/someurl/anothermore should redirect to www.example.com/sub/someurl/anothermore
Edit: I am currently working on a subdirectory so currently its www.example.com/main/someurl to redirect to www.example.com/main/sub/someurl

To redirect everything to the /sub subdirectory, except the root then you can do something like the following at the top of your .htaccess file in the document root using mod_rewrite:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/sub/
RewriteRule !^(index\.php)?$ /sub%{REQUEST_URI} [R,L]
The above states that if the request is not / (or /index.php) and does not start /sub/ (the RewriteCond directive) then redirect to /sub/<suburl>.
Edit: I am currently working on a subdomain so currently its www.example.com/main/someurl to redirect to www.example.com/main/sub/someurl
That's a subdirectory, not a "subdomain". (?)
If this is the case then move these directives to a /main/.htaccess file and change them to read:
RewriteCond %{REQUEST_URI} !^/main/(index\.php)?$
RewriteCond %{REQUEST_URI} !^/main/sub/
RewriteRule (.*) /main/sub/$1 [R,L]

Related

Redirect all the pages to the single page using htaccess?

I am using WordPress and I have to redirect all the pages to the index.html page (I created in the root folder for temporary).
I tried the below code but it's redirecting only the home page.
Redirect 301 "index.html" "https://www.examle.com"
I tried
RewriteRule ^(.*)$ https://www.examle.com/$1 [R=permanent,L] //getting error(The page isn’t redirecting properly)
I tried the below code
RewriteCond %{REQUEST_URI} !/index.html$
RewriteCond %{REMOTE_HOST} !^00.00.00.00 //my ip
RewriteRule $ /index.html [R=302,L]
Any other solution to redirect all pages to index.html? I have more than 50 pages on my website. I want to redirect all on the index.html
You can overwrite your existing .htaccess file content with this. Please note that this code assumes that your wordpress is installed on the root directory.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteRule . /index.html [R=302,L]

Wordpress Multisite Domain Redirects

I have a Wordpress multisite with a primary domain that is used for admin site management and then hundreds of other sites that each have their own unique domain (NOT a subfolder or subdomain of the primary site).
I need to redirect each site to its corresponding landing page on our main site. I am programmatically generating these redirect rules since there are hundreds of redirects that need to be set up. I am then adding those redirect rules to one master .htaccess file at the document root.
Here is what I have so far:
RewriteCond %{HTTP_HOST} ^(.*)?firstsite.com [NC]
Redirect 301 / https://newsite.com/1234/
RewriteCond %{HTTP_HOST} ^(.*)?secondsite.com [NC]
Redirect 301 / https://newsite.com/5678/
What I'm seeing is that it is redirecting all traffic from any of the sites to the first "Redirect 301" address (https://newsite.com/1234/). Presumably because I'm doing "Redirect 301 /" which is redirecting the document root of the entire Wordpress multisite instead of the root of the "RewriteCond" domain specified.
How do I set up essentially a conditional redirect based on the domain being accessed?
I should mention that I do not have Nginx installed on the server.
RewriteCond and RewriteRule work together. So the Redirect 301 was running independently of the RewriteCond. This will work:
RewriteCond %{HTTP_HOST} ^(.*)?firstsite.com [NC]
RewriteRule ^(.*)$ https://newsite.com/1234/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.*)?secondsite.com [NC]
RewriteRule ^(.*)$ https://newsite.com/5678/$1 [R=301,L]
Also, be sure to place this at the very top of your .htaccess rewrite rules. Right underneath the line that says:
RewriteEngine On

.htaccess file to redirect all pages except the wp-admin dashboard

I have redirected the all pages of my wp site from .php extension to /
For example, I am redirect pages such as example.com/about-us.php to example.com/about-us/ but, It is also redirecting my example.com/wp-admin dashboard page which I don't want to redirect.How can I prevent it from redirecting? Here is my .htaccess code.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# redirect to .php-less link if requested directly
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.+)\.php $1 [R=301,L]
Are you sure you want the 301 redirect (R=301)? Surely you want the URL to look like it doesn't have .php but under the hood you still want to access the respective php script?
Anyway, how about this - two conditions:
must not have wp- in the request URI
must end in .php
Like this:
RewriteCond %{REQUEST_URI} !wp-
RewriteCond %{REQUEST_URI} \.php$
RewriteRule ^(.+)\.php $1 [R=301,L,QSA]

Redirecting url to access subdirectory

I have a wordpress subdirectory install in a directory called 'wordpress'. So currently I am able to access the live site as follows: www.domain.com/wordpress.
My objective is for users to simply navigate to: www.domain.com, omitting the wordpress directory from the url.
I was wondering if someone can help me configure my .htaccess so every time a user navigates to anything with domain.com/wordpress it'll redirect to domain.com.
So for example: domain.com/wordpress/admin will become domain.com/admin
I created a new .htaccess file and placed in the root with the following contents, which did not work :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ wordpress [L]
To rewrite / to /wordpress you can use this rule :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$
RewriteCond %{REQUEST_URI} !^/wordpress
RewriteRule ^(.*)$ /wordpress/$1 [L]
This will internally redirect all requests from root to the subfolder.
Do not remove the RewriteCond directive otherwise the the rewrite destination will rewrite back to itself causing an infinite loop error.

htaccess redirect -redirect all pages from root to /blog/ folder

Here is what I'm trying to do:
Redirect all pages in ROOT (ex: ROOT/page-name/) directory only to /blog/page-name/ directory
DO NOT redirect sub directories with NAMES - "/cities/", "/states/", "/categories/" under ROOT directory
I just want to redirect all existing Wordpress pages from ROOT to /blog/ and NOT redirect pages that I will be adding later with /cities/, /states/ and /categories/ names in them.
Please help! thanks!
Add this rule to the htaccess file in your document root, preferably above any rules you may already have there:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(blog|cities|states|categories)
RewriteRule ^(.*)$ /blog/$1 [L,R=301]
You should also be able to do something like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(/ROOT/)([/]?.*)$ [NC]
RewriteRule ROOT /blog/%2 [L,R=301]

Resources