Redirect specific url to homepage - regex

I am building a React (frontend) with Wordpress (Backend - REST API) website. My domain is example.com which is going to have my React app (frontend) and on example.com/backend I have the Wordpress installation.
My problem is: I want to redirect example.com/backend and everything on to example.com except example.com/backend/wp-admin (so i can login). Is that do-able?
In a sentence: Can u redirect example.com/backend but have example.com/backend/wp-admin working (not redirecting)?
I tried lots of reggex in my .htaccess but none would do the trick.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^fanismahmalat.com/backend$ [NC]
RewriteCond %{REQUEST_URI} !^(.)?wp-login.php(.)$
RewriteCond %{REQUEST_URI} !^(.)?wp-admin$
RewriteRule ^(.)$ https://example.com/ [R=301,L]

One way to exclude a URL is to have a rule before the "real" redirect, e.g.
RewriteRule ^backend/wp-admin - [L]
RewriteRule ^backend/ https://example.com [R,L]
The first rule will handle the wp-admin case. The second rule will catch anything else starting with backend/.
Another solution would be to exclude the specific URL from being rewritten
RewriteCond %{REQUEST_URI} !^/backend/wp-admin
RewriteRule ^backend/ https://example.com [R,L]
When the .htaccess file is in the backend sub-folder, the rules are almost the same, but the RewriteRule pattern does not include the backend prefix
RewriteCond %{REQUEST_URI} !^/backend/wp-admin
RewriteRule ^ https://example.com [R,L]
or
RewriteRule ^wp-admin - [L]
RewriteRule ^ https://example.com [R,L]

Related

Using htaccess to get rid of /index.php link?

I can open my site like this:
www.mysite.com
or like this:
www.mysite.com/index.php
I want to create a htaccess rule that redirects www.mysite.com/index.php to www.mysite.com. But all my attempts have other side effects. I've tried:
Redirect index.php home.php
RewriteRule ^index.php?$ home.php [NC,L]
RewriteRule ^index.php/?$ redirect_to_home.php [NC,L]
But all of these mess up the original index.php call. So it does redirect but then the normal mysite.com link doesnt work anymore.
Any ideas?
Could you please try following.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} index\.php
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$
RewriteRule ^ %1 [R=301,L]
Explanation:
Making RewriteEngine On to make the rules work.
Mentioning condition by RewriteCond to REQUEST_FILENAME which checks if mentioned file in browser is present.
Then checking if THE_REQUEST which has complete details of request(including URL and GET/POST method) if it has index.php in it.
Now checking if REQUEST_URI is having index\.php in requested url, where saving everything before it to temp buffer memory to retrive its value later(basically its domain name).
Finally in RewriteRule to redirect complete URL with index.php to till domain name only as per requirement(R=301 is for permanent redirection on browser side).
Use this redirect rule to remove /index.php from any path:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]

mod_rewrite for complex subdomain redirects

These are my current directives for redirecting subdomain HTTP requests to a domain with a path.
RewriteCond %{HTTP_HOST} ^blog.website.com
RewriteRule ^(.*)$ http://website.com/blog [L]
However, this site is a new version of a website for which social media links have been posted. These links have image URLs which are now broken since the URL now points to a different server. The images have since been migrated and the path to said images has been preserved on the new server. In light of this, I need rewrite my directives to account for these legacy links.
I need to write conditions that rewrite the URL only when the URL no path or when the first parameter of the URL is not /wp-content/.
I have to make conditions and rules that differentiate between these 3 URL patterns:
http://blog.website.com
-> http://website.com/blog
http://blog.website.com/wp-content/year/month/image.jpg
-> http://website.com/wp-content/year/month/image.jpg
http://blog.website.com/entry-url-title
-> http://website.com/blog/entry-url-title
This is ultimately how I was able to do it, with a little help from my friends, of course.
RewriteCond %{HTTP_HOST} blog.website.com
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule ^(.*) http://website.com/blog [R=301,L]
RewriteCond %{HTTP_HOST} ^blog.website.com
RewriteCond %{REQUEST_URI} !wp-content
RewriteRule ^(.*) http://website.com/blog/%{REQUEST_URI} [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^blog.website.com
RewriteCond %{REQUEST_URI} wp-content
RewriteRule ^(.*) http://website.com/%{REQUEST_URI} [R=301,NC]

Htaccess - Redirect all but one url

I'm trying to redirect all urls from one domain to another but one (kind of). This is the htaccess I have to redirect all keeping the same url except of the domain (for example domain.com/something goes to domain2.com/something).
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^(.*)$ "http://domain2.com/$1" [R=301,L]
What I want to know is how to redirect all except if the url is domain.com/validation/*
/validation/ is not a subfolder and it has to be the next part of the url after the domain (domain.com/something/validation can redirect, and domain.com/validation/something can't).
I tried a lot of options but none worked :(
I hope is enough information.
You can exclude it in RewriteRule itself:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule !^validation http://domain2.com%{REQUEST_URI} [NE,NC,R=301,L]

Htaccess, How do I redirect this dynamic URL

I have a strange redirect situation that I cannot get to work.
www.example.com/?NR
www.example.com/?PV
I need to get these two URL's rewritten and redirected to the home page but I can't get it to work. Here is what I have tried:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^NR$ [NC]
RewriteRule www.example.com/ http://www.example.com [R=301,L]
RewriteCond %{QUERY_STRING} ^PV$ [NC]
RewriteRule www.example.com/ http://www.example.com [R=301,L]
We bought this site and I have no reference to what those pages might actually be, they just need to be redirected and rewritten properly.
This one rule should work for both of your requirements:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(NR|PC)$ [NC]
RewriteRule ^/?$ /? [R=301,L]
Remember that RewriteRule only matches REQUEST_URI which is URI part without domain name and query string.
/ in target URI is for your home URL and ? in the end is to strip any existing query string.
Reference: Apache mod_rewrite Introduction

.htaccess issue with ExpressionEngine and YOURLS

I have a primary site running ExpressionEngine and I am trying to get YOURLS running in a subfolder called "work", ie: http://foo.com/work/ for short urls and EE is running at the root, ie: http://foo.com/. Please note there is no 'www'.
The problem I am having is that when I use a short URL and a user adds "www" to the URI, such as http://www.foo.com/work/123 I get a redirect chain that looks like this:
http://www.foo.com/work/123 302 redirects to
http://www.foo.com/work 301 redirects to
http://www.foo.com/work/ which returns 200
Everything works fine if you omit the 'www' from the URI. YOURLS is set up to use the non-www but the worry is that some users might habitually type the 'www' as these URIs are being placed on printed ads.
The root .htaccess file looks like this:
SetOutputFilter DEFLATE
DirectoryIndex index.php index.html
# BEGIN ExpressionEngine Rewrite
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.foo.com [NC]
RewriteRule ^(.*)$ http://foo.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteRule ^(.*)$ /index.php?$1 [NC,L]
</IfModule>
# END ExpressionEngine Rewrite
The /work/ (YOURLS) .htaccess looks like this:
# BEGIN YOURLS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /work/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /work/yourls-loader.php [L]
</IfModule>
# END YOURLS
How can I get the 'www' URIs to work like the non-www URIs do without completely hosing EE? :)
I faced a similar issue. Some end users or people publishing the short urls tend to append them with www while the short url was bought with the intention of using it without www.
I also installed the YOURLS redirect index plugin to forward homepage visitors to our regular homepage.
So I had a few different types of urls:
short domain homepage, should lead to the regular https homepage
short domain YOURLS admin panel, should lead to the https admin panel
short domain shortened urls, should lead to the long url
I wanted all urls to work with or without www and make sure http was forwarded to https.
My solution was to start .htaccess with these lines:
# Force HTTPS (because of passwords via /admin) and use clean domain (non www)
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www.examp.le [NC]
RewriteRule ^(.*)$ https://examp.le/$1 [L,R=301]