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

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]

Related

Redirect with .htaccess to exact URL provided

So I want to redirect from old sites url to new one. Lets say example.com/en/some/stuff/foo/bar needs to be redirected to example.com/some/stuff.
Here is what I have at he moment:
Redirect 301 /en/some/stuff/foo/bar/ /some/stuff/
The problem is that I end up being redirected here example.com/some/stuff/foo/bar, but I need as I defined inside .htaccess example.com/some/stuff.
How to redirect properly to exact URL I have provided without anything extra.
You can use this redirect rule with regex in your site root .htaccess:
RewriteEngine On
RewriteRule ^en/(some/stuff)/.+ /$1 [L,NC,R=301]
Make sure to test it in a new browser to avoid old cache.

Redirect subfolder to another subfolder without appending parametres

I need to redirect a subfolder to another subfolder, but I don't want to carry over what comes after the trailing slash or any URL parametres.
For example:
www.mysite.com/this-is-the-old/ -> www.mysite.com/this-is-the-new/
But I would like this redirect to fire no matter what the user adds to the end of the old URL. No matter what I try (in either RedirectMatch or RewriteRule) it's always adding the trailing characters to the new URL.
You can use this rewrite rule in your site root .htaccess:
RewriteEngine On
RewriteRule ^this-is-the-old(/.*)?$ /this-is-the-new/? [L,NC,R=301]
Make sure to clear your browser cache completely before you test this new rule.

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

Mod Rewrite rules for redirecting a single subdirectory and its contents

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.

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