301 redirect, conflicting with RewriteRule - apache

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

Related

How do I redirect just my home page and keep rest of the URLs as it is?

I need to redirect some URLs as they are pointing to same page. For example:
https://www.example.com/home
https://www.example.com/Home
https://www.example.com/index.php
These all URLs need to refirect to:
https://www.example.com/
So I am using this in .htaccess file it is working fine :
Redirect 301 /home https://www.example.com/
But the issue is when I try to access the URL like this:
https://www.example.com/home/sub
It redirects to resulting in 404:
https://www.example.com//sub
I need this URL to work as it is instead of 404 or any redirect:
https://www.example.com/home/sub
Redirect 301 /home https://www.example.com/
Because the mod_alias Redirect directive is prefix-matching and everything after the match is copied onto the end of the target URL. So, when you request /home/sub, /sub is copied on to https://www.example.com/, resulting in https://www.example.com//sub.
I imagine you also have mod_rewrite directives. You should avoid mixing redirects from both modules, so try the following instead using mod_rewrite at the top of the root .htaccess file:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(home|index\.php)$ / [R=301,L]
The condition that checks against the REDIRECT_STATUS environment variable ensures that only direct requests are processed, as opposed to internally rewritten requests which will result from having a front-controller pattern (which I assume you have later in the file).
You will need to clear the browser cache since the erroneous 301 (permanent) redirect will have been cached by the browser. Test with 302s to avoid potential caching issues.
Reference:
https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule

How to get mod_rewrite to redirect internally

I have been searching since yesterday to find a solution to my problem.
This is my current .htacces file:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^l/(\w+)/?$ sites/$1 [L] # Handle shortlink
I would like http://drop2upload.danschalow.com/l/58032bf6b1917/ to point to http://drop2upload.danschalow.com/sites/58032bf6b1917/.
Now, if you click on http://drop2upload.danschalow.com/l/58032bf6b1917/ you will find that you are returned a page. What you get however, is a 301 Moved Permanently, which means the user 'sees' that he has been redirected.
How do I redirect the user internally, without him knowing.
Cheers,
Dan

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

Making sure my rewrite rule is a 301 re-direct

I have the following re-write rule which directs krmmalik.com to krmmalik.com/me
How do i make sure this rule is a 301 re-direct, and if it isnt one already, how can i turn it into one?
I've tried using the mixing and matching the tips from this site
http://www.webweaver.nu/html-tips/web-redirection.shtml
as well as Google's Support Articles and existing SO questions, but not having much luck. Note the re-write rule in itself so far has been working fine.
I've also added a CNAME for "www" to "krmmalik.com" in my DNS file. Is that good enough, or do i need to add a specific 301 redirect for that as well?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?krmmalik.com$
RewriteRule ^(/)?$ me [L]
Try the following:
RewriteRule ^/?$ /me [L,R=permanent]
The R=permanent flag instructs a 301 status redirect (and you can use R=301 if you prefer, but I think that "permanent" is more readable).
Putting a forward-slash at the start of the /me target URL will tell Apache to redirect the user to the directory named "me" at the web server's public root directory. So in your case it should redirect the user to krmmalik.com/me (or www.krmmalik.com/me).
Also, you don't need to wrap the match pattern in parentheses, because you don't need to capture the slash for later use. So ^/?$ will do the job fine.

Make index.cgi redirect to Apache webserver document root

I'm trying to expose a CGI file as my document root and web server. I do not want to expose the fact that the server is running a CGI script.
How can I map a URL http://host/index.cgi/ back to http://host/ in Apache2? I'm guessing it involves mod-rewrite, but I haven't finished grokking all the docs yet.
The following configuration is working, but I'm guessing there is a more complete solution:
RewriteEngine ON
Redirect /index.cgi/ /
but I'm guessing there is a more complete solution:
If you want to redirect requests for /index.cgi/ to /, then there's really no need for anything else. Note that RewriteEngine On is part of mod_rewrite, and the Redirect directive is part of mod_alias. You don't need the rewrite engine for the redirect to work. Also note that this is a 302 redirect, and it isn't a permanent one. You probably want to include either the 301 or permanent keywords in there:
Redirect 301 /index.cgi/ /
Additionally, the Redirect directive links two path nodes together, so any further paths appended to the source gets appended to the destination. So given the above, if you go to:
http://host/index.cgi/some/more/path.txt
The browser will get redirected to:
http://host/some/more/path.txt
If you don't want this, you can change the Redirect to a RedirectMatch and use a regular expression:
RedirectMatch 301 ^/index\.cgi/$ /
Correct solution using mod-rewrite.
Update CGI script to rewrite URLs to desired path. This means that URLS like index.cgi/path change to /app1/path or in my case, just /path. For my script there was an option called virutal-root that made a global change.
Then udpate Apache with the following directives. What it does, is internally rewrite all the URLs to include the CGI script, but ignore URLs going to the actual script.
RewriteEngine ON
RewriteCond %{REQUEST_URI} !^/index.cgi
RewriteRule ^(.*)$ /index.cgi/$1 [PT]
One sticking point, is that if the CGI script references any static files on the server, they need to be placed in a path that is also out of the rewrite rule. So for example, exclude another path, before the RewriteRule:
RewriteCond %{REQUST_URI} !^/_static