Apache redirect everything under a folder except the folder itself - apache

At some point in the past, our site had a structure like www.example.com/departments/it or www.example.com/departments/asdf. Then, the the paths changed to something like www.example.com/it or www.example.com/asdf, getting rid of the 'departments'.
There is content that now lives under the /departments/ path, so using the following rewrite rule doesn't work
RewriteRule ^/departments/(.*)$ /$1 [R=301,L]
Because while it rewrites everything under departments, departments itself gets redirected back to the site root.
How do I prevent this so that if I go to www.example.com/it it works, www.example.com/departments/it redirects to www.example.com/it, and if I go to www.example.com/departments/, I get the department information?

Paste this in and give it a try
RewriteRule ^/departments/it$ /it/$1 [R=301,R,L]

Related

.htaccess RewriteRule based on anchors

I am trying to update a RewriteRule. Previously, the redirect looked like this:
https://mywebsite.com/docs/#/en/introduction → https://manual.mywebsite.com/#/en/introduction
I would like to use /docs/ for something else now but I would like to keep redirecting requests containing a forward slash after the # to the manual subdomain. This is what I would like to achieve:
The old redirects continue working as usual:
https://mywebsite.com/docs/#/en/introduction → https://manual.mywebsite.com/#/en/introduction
This would not get redirected since there is no forward slash following the #:
https://mywebsite.com/docs/#overview
Here is what I have:
The .htaccess file containing the following existing rule which redirects everything:
RewriteRule ^docs/(.*) https://manual.mywebsite.com/$1 [L,R=301]
I tried this but it did not work (I tried with https://htaccess.madewithlove.com/ which says that my rule does not match the URL I entered):
RewriteRule ^docs/#/(.*) https://manual.mywebsite.com/#/$1 [L,R=301]
I also read about the NE (no escape) flag (https://httpd.apache.org/docs/2.2/rewrite/advanced.html#redirectanchors) which did not help either.
I am also sure that the server is actually using the file.
To summarize, my problem is that I want to match a URL containing /docs/#/ and redirect it to a subdomain, keeping the /#/ and everything that follows it.
Anchors are not part of the URL that's transmitted to the server: They stay within the browser, thus you can't build a rewrite rule that take anchors into account.
Inspect your access-logs to see what you get

Redirect and Rewrite Rule for Apache

I am trying to install a CalDAV client on my apache webserver, and I am having trouble using a combination of redirects and rewrite rules to get a desired url.
The document root for my webserver is /var/www, and the calendar files are stored in /var/www/agendav/web/public. If I go in through my browser at <website>/agendav/web/public/index.php, I have no trouble getting to the interface and using it, so that is not a problem. However, my desired URL for the calendar is <website>/calendar/, instead of having to go down through the agendav folder tree. I have been trying to perform this with a redirect rule, and a rewrite rule, but to very little success. I have found a few other answers here that have gotten me close, such as this one and this one, with a working redirect, but I am still having issues with the rewrite rule. Here is the current solution I have:
# Rules for the Calendar
Redirect "/calendar" "/agendav/web/public"
RewriteEngine On
RewriteCond %{REQUEST_URI} "^/agendav"
RewriteRule "^/agendav/web/public/(.*?)$" "/calendar/$1"
My current solution seems a little circular. First, I redirect the user to the agendav folder, then then try to hide the redirect behind a rewrite rule, when it seems that I could just get away with a single rewrite rule. Unfortunately the group I am working for is not big enough to have their own dedicated server manager, and I ended up with the job despite knowing very little about it. Any help to get this would be greatly appreciated.
You don't need the redirect, you can do it with just a rewrite rule, the problem was that you have the condition and the rule reversed, you were using the real path for the conditions instead of the "virtual" path:
RewriteEngine on
RewriteCond %{REQUEST_URI} "^/calendar" [NC]
RewriteRule "^calendar/(.*)" "/agendav/web/public/$1"
With this rules all requests for http://yourwebsite/calendar/* are internally served with http://yourwebsite/agendav/web/public/*.

.htaccess rewrites root to subfolder, yet subfolder app 302 redirects right back to full path

I have a standard .htaccess RewriteRule that silently rewrites any request for webroot into a subfolder which contains a MantisBT installation. So the user types in "example.com" and my server secretly serves them files from "example.com/path/to/mantisbt".
The problem now is that MantisBT's index page immediately does some authentication based logic routing and sends a 302 redirect to the FULL "example.com/path/to/mantis/login", which subverts my rewriting. I'm trying to have everyone access my MantisBT installation as if it resided in the webroot.
Now, I'm aware that after MantisBT's 302 redirect to the full path, I could redirect them AGAIN back to webroot. But redirecting people twice every time MantisBT goes through some routing logic seems like a dirty hack. I also know that I could hack up the MantisBT code, but I hate re-hacking code every time a new version comes out.
So, is there a way to trick MantisBT (or any other app for that matter) into thinking it resides in root, and therefore crafts it's redirect paths based on a webroot-relative url? For example: "example.com/login" instead of "example.com/path/to/mantis/login".
I'd really prefer to resolve this using an Apache .htaccess method, or httpd.conf change. Perhaps DocumentRoot or RewriteBase?
Try adding this rule above the internal rewrite rule that you had before
RewriteCond %{THE_REQUEST} \ /+path/to/mantisbt/([^\?\ ]*)
RewriteRule ^ /%1 [L,R]
this redirects the browser when the browser directly requests anything in /path/to/mantisbt/. Then the rule that you already have to internally rewrite into the mantisbt directory would take effect.

Need Top Level 301 Redirect but subdomain has same name as redirect?

I have a main site with multiple subdomains below it:
so:
example.com
products.example.com
books.example.com, etc.
I'm trying to create a redirect like:
RedirectMatch 301 ^/products/catalog/(.*) //products.example.com/$1
But this doesn't work because at "products" is a subdirectory/subdomain so it just creates a redirect loop at //products.example.com
But This works (because /store isn't //store.example.com/)
RedirectMatch 301 ^/store/catalog/(.*) //products.example.com/$1
I tried experimenting with a rewritecond to have it only redirect if the url contains the root domain, but that didn't work either.
If a page like:
//products.example.com/products/great-thing.html exists and is a subdomain under a main domain installed in a folder "products" (i.e. /public_html/products/...)
-- how would you redirect from:
//example.com/products/great-thing.html to
//products.example.com/products/great-thing.html
I hope that makes sense!
I should add that this site is hosted on a managed cloud/vps - so there may very well be something in their setup that is causing trouble.
But at the end of it there seems to be a problem creating redirects when a folder/subdomain exists with the same name...
This was hard to track down and may have been more of a processwire cms issue than a straight apache redirect issue, but in case it is helpful to someone, what worked was:
Top level (i.e. root domain site) needed for
//example.com/products/great-thing.html to //products.example.com/products/great-thing.html
RewriteRule ^(.)/products/(.) https://products.example.com/products/$2 [R=301,L]
which should have gone to:
https://products.example.com/products/great-thing.html
, but instead created a loop which never resolved.
But it does work when on the subsite (processwire) this was added:
RewriteRule ^products/(.*) ^/products/$1 [R=301,L]

Site Redirection with htaccess creates an infinite loop

Currently we have a number of sites hosted in one GoDaddy account. Each site is inside a separate folder and their respective domains are bound to those folders. The problem is that our main site is in the root of the host and our primary domain is linked to this root. The problem with this setup is that if for example, one of my other sites is in a folder called "secondsite", I can reach the website by going to www.secondsite.com (which is fine) but ALSO by going to www.mainsite.com/secondsite, which we absolutely not want.
The idea is to move all the files of the main site to a folder of their own (let's call it "mainsite"). When I talked to GoDaddy they told me to do a 301 redirect to that folder. I have never worked with .htaccess but I looked up how to redirect and found that I needed to write this:
Redirect 301 / http://mainsite.com/mainsite
However if I do that when I try to access the website I get infinite redirection: http://mainsite.com/mainsitemainsitemainsitemainsitemainsitemainsitemainsitemainsitemainsitemainsitemainsitemainsite
I've looked online and tried to use other solutions, like
RedirectPermanent / http://mainsite.com/mainsite
but the effect is the same.
Not sure what I'm doing wrong.
When using Redirect, you're linking path-nodes together, So:
Redirect / /abc/
means, anything starting with / will go to /abc/, e.g.:
/ -> /abc/
/foo -> /abc/foo
/1/2/3/4/5 -> /abc/1/2/3/4/5
And thus, since you're redirecting back to the same host, the / captures everything and you've got an infinite loop.
Try using either RedirectMatch:
RedirectMatch 301 ^/(?!mainsite)(.*)$ /mainsite/$1
or us mod_rewrite:
RewriteCond %{HTTP_HOST} ^(www\.)?mainsite\.com$ [NC]
RewriteRule ^(?!mainsite)(.*)$ /mainsite/$1 [L,R=301]