Mod Rewrite rules for redirecting a single subdirectory and its contents - apache

I have the following folder structure:
/
/backend + sub-pages (these are the ones I want to hide)
/backend/admin + sub-pages
/backend/api + sub-pages
At the moment, the CMS generates a whole load of pages under the folder /backend, which I don't want to be visible to the public.
I require that any route /backend/* except for any route in the /backend/admin sub-folder or backend/api sub-folder, is redirected to /backend.
Is this possible using mod rewrite in an htaccess file?

Assuming that mod_rewrite is properly enabled, you can use the following rule:
RewriteCond %{REQUEST_URI} !^/backend/(admin|api)
RewriteRule ^backend/. /backend [R,L]
The condition prevents anything from happening if admin or api is behind 'backend'. Otherwise any url that starts with backend/ and at least a single character following that will be redirected to backend. This is mainly to prevent redirect loops. Once you have tested this, and it works as you expect, you can change [R,L] to [R=301,L] to make the redirect permanent.

Related

Redirect Page Path to Homepage using .htaccess

How can I redirect all of the sub pages that include a particular path (/home-2016/) back to my home page? I am using the below code in my .htaccess file, but it is not working. I can't seem to find a solution.
I want to redirect the following
https://www.example.com/home-2016/ to https://www.example.com
https://www.example.com/home-2016/... to https://www.example.com
RewriteRule ^home-2016/(.*) / [R=301,NC,L]
RewriteRule ^home-2016/(.*) / [R=301,NC,L]
This rule already does as you require, but it would seem you were putting the rule in the wrong place. Redirects like this would need to go near the top of the .htaccess file, before any existing rewrites (like a CMS front-controller pattern).
However, this rule can also be simplified, since you don't need the capturing subpattern.
The following will do the same, but with a more efficient regex:
RewriteRule ^home-2016/ / [R=301,NC,L]
Unless you specifically need a case-insensitive match, you should remove the NC flag.

Htaccess Redirect directory to another but allow the main folder URL to stay the same

We are currently using the below .htaccess code to redirect /artist/* to /artists/* which works well however, we need the URL /artist/ to remain available and not redirect through to /artists/ is this possible with .htaccess? We'd like to avoid a PHP based redirect if possible.
RewriteRule ^artist/(.*)$ /artists/$1 [R=301,NC,L]
If you don't want to change URL in your browser means you don't want to redirect and only want to rewrite to different URL then try following(we need to remove R flag from rules. Also this rule considers that you have /artists in your root directory if that's not the case then remove its starting slash in Rule.
RewriteRule ^artist/(.*)/?$ /artists/$1 [NC,L]

Redirect all pages from old domain to new specific URL without trailing slash content

I've got an old domain, let's say oldomain.com where I need to redirect all traffic to a specific URL, newdomain.com/path
While redirects from oldomain.com go perfectly, anything with content after the trailing slash will be copied over in the newdomain url structure causing 404's.
For example visiting: oldomain.com/somepage will result in newdomain.com/pathsomepage
What I'm looking for are some rewrite rules that will redirect any and all traffic from oldomain.com to newdomain.com/path without changing the specific "newdomain.com/path" URL.
I'm currently using the rules bellow which leads to the result above:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldomain.com
RewriteRule ^(.*) https://newdomain.com/path [P]
PS: the redirect is going to a Magento store.
You are trying to reverse-proxy in your directives (the P flag in the rewrite), but since you are describing a redirect... In the old virtualhost you just need to add a simple Redirect directive like this:
Redirect / https://newdomain.example.com/
This will Redirect all requests no matter how they are made to the new domain. Example GET /something will be redirected to https://newdomain.example.com/something
If you want the target to be a fixed destination like https://newdomain.example.com/path no matter what, use RedirectMatch instead:
RedirectMatch ^ https://newdomain.example.com/path

Apache mod_rewrite links proxy

I want to make a proxy for external links with apache's mod_rewrite module.
I want it to redirect user from, ie http://stackoverflow.com/go/http://example.com/ to http://example.com/ where http://stackoverflow.com/ is my site's URL. So I added a rule to .htaccess file.
RewriteRule ^/go/http://(.+) http://$1 [R=302,L]
But it doesn't work at all. How to fix this?
I am not sure if Apache or the browser reduces // to /, but since it doesn't change the directory one of them reduces this to a single slash on my setup. That's why the second slash has a ? behind it in the rule below:
RewriteRule ^go/http://?(.*)$ http://$1 [R,L]
This will redirect the user to that domain.
This will rewrite all urls (without the beginning http://) to new complete URL. If you're gonna use https links also, you need something like the second rule.
RewriteRule ^go/(.*) http://$1 [R=302,L,QSA,NE]
RewriteRule ^gos/(.*) https://$1 [R=302,L,QSA,NE]
I also added the QSA if your need to include parameters

301 redirect, conflicting with RewriteRule

We did some maintenance today, and moved our web forums from /forums into the root folder of the domain.
We put in a redirect 301 in a .htaccess file:
Redirect 301 /forums/ http://www.ourforums.com/
However, we used to have some links that contained duplicate /forums folders. I.e. www.ourforums.com/forums/forums/forum.1
Obviously the redirect from above now leads to /forum.1, which odes not exist. I would like the old link to actually point to www.ourforums.com/boards/forum.1. I attempted to use something like:
RewriteRule ^/forums/forums http://www.ourforums.com/boards/ [NC,R=301,L]
Regardless of what I tried though, the Redirect seems to supersede any RewriteRules I put in the same file, regardless of whether I place them before the Redirect.
Is there any way I can somehow ensure the RewriteRule is handled before the Redirect?
This is because mod_alias (the Redirect directive) and mod_rewrite (the RewriteRule directive) is conflicting with each other in your case. Both of them play their part in the URL-file-mapping processing pipeline, where URI's get processed, rewritten, flagged, and eventually mapped to a resource and a response. You have a Redirect rule which is getting applied and the response is being flagged as a redirect. The thing about the Redirect directive is that it connects 2 path nodes together, meaning:
/forums/
is connected to
http://www.ourforums.com/
So anything below the /forums folder ends up getting redirected as well. This is why it's catching ^/forums/forums.
You can either stick to just mod_rewrite or use a RedirectMatch that excludes /forums/forums:
RewriteRule ^/forums/forums(.*)$ http://www.ourforums.com/boards$1 [NC,R=301,L]
RewriteRule ^/forums/(.*)$ http://www.ourforums.com/$1 [NC,R=301,L]
or
RedirectMatch 301 ^/forums/(?!forums)(.*)$ http://www.ourforums.com/$1
Manually adding redirect statements like so seems to do the trick for me:
Redirect /forums/forums/forum.1 http://www.ourforums.com/boards/forum.1
I had a somewhat similar problem. I was trying to add redirects from cpanel while I already had some rewrite rules written in my .htaccess file. The error I got was "No maching tag for " What I ultimately did was that kept a copy of my existing rules and cleaned the .htaccess. Then went and added all the redirects that I needed from cpanel, and then at the end put back my own rewrite rules in the end of the file. That worked for me