Redirect subfolder to another subfolder without appending parametres - apache

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.

Related

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]

How to remove a subfolder from URL using htaccess

We have those kinds of URL:
https://www.example.com/public/something/something
I need to get rid of the /public/ subfolder so every duplicate URL with this subfolder would be redirected using 301 redirect to the same URL but without the subfolder. The example above would be redirected to:
https://www.example.com/something/something
I have tried this:
RewriteRule ^public/(.*)$ /$1 [L,NC,R=301]
The example still returns 200. How to redirect every URL with /public/ to the same URL without the subfolder? And if It possible, could you explain the .htacces code which handles it briefly?
EDIT: We have discovered another hidden .htaccess file on the server which appears to be the one we need to set up. Here is its content: https://gist.github.com/zatkoma/6425eb2f93c3c6affd46828de044e95b.
We have added the line here but still nothing happens. Where exactly should we put the line?
Thanks
There are two problems in your config: the first is that when in subfolder .htaccess, RewriteRule pattern will strip the path to that subfolder
so ^public/ is never matched.
The second is little tougher: your requirement that your site is in located in /public subfolder, so when user enters URL without /public, you (in root htaccess) rewrite that to /public/...
But at the same time you want, if someone writes URL starting with /public/, you want it to be removed.
This can be achieved by checking if Original request (as typed in browser) contained /public.
So now the flow is:
Check if request has /public, if not then add it internally, and go
to /public (done by htaccess in root)
When in /public (thus having subfolder .htaccess in effect), don't check if request begins with /public (because it always does), but check if original request (before step 1). had /public, and only if it had, do the rewrite to URL without /public
RewriteCond %{THE_REQUEST} \s/public/
RewriteRule (.*) /$1 [L,NC,R=301]

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.

Exclude a URL from folder redirection

I am redirecting all URLs from www.example.com/forums to www.example.com/blog/.
so I made this rule in .htaccess:
RewriteRule ^forums blog/$1 [L,R=301]
the thing is that I want to exclude some URLs that also begin with forums/ and redirect them to particular URL other than /blog.
For example, forums/8/some-made-up-word-here-1681 to /studies/some-made-up-studies.
Right now, it redirects to /blog like all URLs that start with forum/
You just need to include the more specific redirects first, before the more general rule. For example:
RewriteEngine On
# Specific redirects
RewriteRule ^forums/8/some-made-up-word-here-1681 /studies/some-made-up-studies [R,L]
# Redirect all other URLs that start /forums
RewriteRule ^forums/?(.*) /blog/$1 [R,L]
I've also modified your existing directive to redirect /forums/<something> to /blog/<something>, which I assume was perhaps the original intention, since you were using a $1 backreference in the substitution, but did not have a capturing group in the RewriteRule pattern. Your original directive would have redirected /forums/<something> to /blog/.
I've also included a slash prefix on the substitution. This is required for redirects, although you may have set RewriteBase instead, in which case you do not need to do this.
You will need to clear your browser cache before testing, since the earlier catch-all 301 will have been cached hard by the browser. For this reason it is often easier to test with temporary 302s in order to avoid the caching problem. Change the above temporary redirects to 301s only after you have confirmed this is working as intended.
UPDATE: To redirect all URLs that start /forums to /blog/, without copying the remainder of the URL, then change the last directive to read:
# Redirect all other URLs that start /forums
RewriteRule ^forums /blog/ [R,L]
Basically, the $1 in your original directive was superfluous.

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