Htaccess single page redirect problems - .htaccess

I'm having some issues setting up single page redirects using htaccess. Currently I have a htaccess file with:
RewriteCond %{SCRIPT_FILENAME} !-s
RewriteRule (.*) index.php?path=$1 [QSA,L]
Then a set of redirects e.g.
Redirect 301 /oldpage.htm http://www.mydomain.com/new-page
But the problem I am having is that when I go into a browser and type in the old URL, I get redirected to URL with a parameter attached, e.g:
"http://www.mydomain.com/new-page?path=oldpage.htm"
For some of my redirects this seems to work anyway, for some it produces a 404 error. I've also tried using RewriteCond and Rewrite Rule to write more generic catch all redirects for those pages that I can, and I'm having the same issue.
I'm thinking that some other rule must be interfering with my redirects - the only one I can see which might do so is the rule above, but if that was the case shouldn't the URL I am redirected to end up being
"http://www.mydomain.com/index.php?path=oldpage.htm"
Can anyone explain why parameters are being appended to the URLs and how I can stop this happening so that my redirects work?
Cheers!

This is because mod_alias (the Redirect directive) and mod_rewrite (the Rewrite* directives) are both being applied to the same URI in the URL-file mapping processing pipeline. In order to keep this from happening, you need to stick with one or the other in this case.
You also want the redirect to get applied first:
RewriteRule ^/?oldpage.htm$ http://www.mydomain.com/new-page [L,R=301]
RewriteCond %{SCRIPT_FILENAME} !-s
RewriteRule (.*) index.php?path=$1 [QSA,L]
Your index.php rule is going to catch all requests that aren't CGI scripts that are symlinks, so, pretty much everything if that's really your intention. Otherwise you can let legit requests get by unscathed by including:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Above the last rule.

because your $1 in rewrite rule
RewriteRule (.*) index.php [QSA,L] try this

Related

Apache redirect directive not acting as final rule

I am trying to achieve a simple redirect - from /news to /insights
I have the following in my .htaccess file:
redirect 301 /news /insights
<IfModule mod_rewrite.c>
RewriteEngine On
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>
Whenever I've used the redirect directive before, any matched URLs would be redirected and no further rewrites in the file would be processed. That is to say, going to /news would send you to /insights, and the rewrite to index.php would not be processed.
However, with this current setup, going to /news sends me to /insights?p=news, so for some reason the rewrite to index.php is still being processed.
Furthermore, if I comment out the index.php rewrite, then I get sent to /insights as expected.
This isn't how I've usually experienced this working so am unsure why it's doing this.
I have also tried the following:
RewriteEngine On
RewriteRule "^/news" "/insights" [R=301,L]
This simply results in a 404 instead of redirecting, which I also do not understand.
I am aware I could do the following:
RewriteEngine On
RewriteCond %{REQUEST_URI} "^/news"
RewriteRule ^ /insights [R=301,L]
which does work, however, I don't really want to have multi-line rewrites for lots of URLs, and would like to understand why the other 2 examples do not work.
You just need to insert this rule before last catch-all rule.
RewriteRule ^/?news/?$ /insights [R=301,L,NC]
Place it just below RewriteEngine On line so that mod_rewrite engine executed this rule before other rule.
Make sure to test it in a new browser.

Simple rewritecond in htaccess doesn't work as expected

This is super simple but it's driving me crazy! I have a website at http://example.org/ and a subdirectory at http://example.org/ccc/
I want to redirect anything outside of the /ccc/ directory to a different website.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/ccc/?.*
RewriteRule ^(.*)$ https://new-website.com/$1 [L]
But this code doesn't work, it redirects the /ccc/ directory. According to my research and testing with this htaccess tester, it should not redirect because the RewriteCond is checking against /ccc with optional slash and other characters after it.
What is happening? Does this look correct?
Edit: This method from this answer is also not working, the CCC domain is being redirected:
RewriteEngine on
RewriteRule ^ccc index.php [L]
RewriteRule (.*) https://new-website.com/$1 [R=301,L]
PHP 5.4.45, Apache/2.2.31
Assuming ccc/ directory doesn't have a separate .htaccess, you may use this rule:
RewriteEngine on
RewriteCond %{THE_REQUEST} !\s/ccc[/?\s] [NC]
RewriteRule ^ https://new-website.com%{REQUEST_URI} [L,R=301,NE]
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite directives. An example value of this variable is GET /index.php?id=123 HTTP/1.1
It looks like [L] isn't behaving normally and I'm guessing it's the old version of Apache (2.2.31) because these rules worked on a separate website. I found this solution which seemed to work for this case, the third line below:
RewriteEngine on
RewriteRule ^ccc/? index.php [L]
RewriteCond %{ENV:REDIRECT_STATUS} != 200
RewriteRule ^(.*)$ https://new-website.com/$1 [L]
Explanation from that question:
The problem is that once the [L] flag is processed, all the next RewriteRules are indeed ignored, however, the file gets processed AGAIN from the begin, now with the new url.
This magic Condition will not process the catch all if the file was already redirected.

Set up of conditional redirect in htaccess

I've been asked to make an existing web site multi-language.
In preparation for this I have had to move all existing pages from /path/page to /en/path/page
To maintain any existing incoming links I now need to set up an htaccess redirect to send any requests from their original urls to the new /en/path/page urls but I'm having trouble getting this to work.
This is what I currently have;
RewriteCond %{REQUEST_URI} !^/en$
RewriteRule ^(.*)$ /en/$1 [R=301,L]
Which I think is meant to check the requested URI and if it doesn't begin with /en then prepend /en onto the requested URI... but I'm obviously mistaken since it doesn't work.
Any help appreciated. Thank you.
UPDATE.
Since this is an ExpressionEngine site and there is an additional rule to remove the index.php portion of the URL here are both rules
# Rewrite for new language based urls
# This is to try and get all current pages going to /en/(old url) with a 301 redirect
RewriteCond %{REQUEST_URI} !^/en(/.*)?$
RewriteRule ^(.*)$ /en/$1 [R=301,L]
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png|ico)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
I have also tried this with the language rewrite after the index.php one. I'm still getting stuck in loops.
What it does is, checking whether the URI is not exactly /en, since the $ indicates the end of the string right after en.
Try this, it checks whether the URI is not exactly /en or /en/ or doesn't start with /en/, and if that's the case it will prepend /en/:
RewriteCond %{REQUEST_URI} !^/en(/.*)?$
RewriteRule ^(.*)$ /en/$1 [R=301,L]
update Considering the other rules you have in your .htaccess file, it is necessary to have the language rule not match again for the following internal redirect to /index.php..., otherwise you'll end up with an endless loop.
There may be better ways to prevent this, however the first thing that comes to my mind would be checking for index.php in the first condition:
RewriteCond %{REQUEST_URI} !^/(index\.php|en)(/.*)?$
So this will cause the rule not to apply after the internal redirect. But be careful, this solves the problem for this specific case only in which the internal redirect goes to index.php!

Htaccess non-www and strip index.php in a nice way

I'm having some trouble with my .htaccess redirections.
I want a situation in which the (non-www)domain.tld is redirected to the www.domain.tld. And I want to rewrite the arguments to skip the index.php, making a request for /foo go to index.php/foo.
Initial situation
First I had these rules
RewriteCond %{HTTP_HOST} ^domain\.tld [NC]
RewriteRule ^(.*)$ http://www.domain.tld/$1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_\ /:-]*)$ index.php [L]
And this worked. Mostly. What didn't work was that in PHP $_SERVER['PATH_INFO'] stayed empty and I disliked the whitelisting of the characters.
Change for PATH_INFO and to accept more
So I changed the last line into this:
RewriteRule ^(.*)$ index.php/$1 [L]
This fixed the PATH_INFO and the limited characters. However, I recently noticed that this caused the non-www redirect to www. to fail miserably.. When going to the non-www domain Apache says
Moved Permanently
The document has moved here.
Where 'here' is linked to the same thing I typed (non-www domain.tld) and thus failing to serve the user.
Continuing the search..
I found a lot of Q&A here and elsewhere or the topic of non-www redirections, but all seem to fail in some way. For example:
RewriteCond %{HTTP_HOST} !^www.*$ [NC]
RewriteRule ^/.+www\/(.*)$ http://www.%{HTTP_HOST}/$1 [R=301]
This just didn't do that much. Nothing got redirected, although the website was served on the non-www.
Anyone knowing what I do wrong or having a solution for this mess? :)
(Preferably, I would like the non-www redirection to be global. So that I don't have to change the actual domain name every time.)
I guess you’re just missing the L flag to end the rewriting process:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
And make sure to put this rule in front of those rules that just cause an internal rewrite.

Send 404 when requesting index.php through .htaccess?

I've recently refactored an existing CodeIgniter application to use url segments instead of query strings, and I'm using a rewriterule in htaccess to rewrite stuff to index.php:
RewriteRule ^(.*)$ /index.php/$1 [L]
My problem right now is that a lot of this website's pages are indexed by google with a link to index.php. Since I made the change to use url segments instead, I don't care about these google results anymore and I want to send a 404 (no need to use 301 Move permanently, there have been enough changes, it'll just have to recrawl everything).
To get to the point: How do I redirect requests to /index.php?whatever to a 404 page? I was thinking of rewriting to a non-existent file that would cause apache to send a 404. Would this be an acceptable solution? How would the rewriterule for that look like?
edit:
Currently, existing google results will just cause the following error:
An Error Was Encountered
The URI you submitted has disallowed
characters.
I tried something like:
RewriteRule ^index\.php?(.*)$ /no-exist.html [R=404,L]
But it caused an internal server error.
edit2:
CodeIgniter is already sending '400' errors, will this be sufficient to get my pages removed from google?
RewriteRule's R[=code] flag allows code only from range 300-400.
Don't use redirect R flag - just try to rewrite to an unexciting page:
UPDATED:
Two redirects are apposed to each other - use RewriteConds to escape the interference.
Complete .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/index.php.*
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ /no-exist.html [L]
RewriteCond %{REQUEST_URI} !^/index.php.*
RewriteCond %{REQUEST_URI} !^/no-exist.html.*
RewriteRule ^(.*)$ /index.php/$1 [L]
Note: /no-exist.html actualy doesn't exist. Suppose, it will help you.
There is a special HTTP status code 410 GONE to tell the World to remove resource:
The requested resource
/index.php
is no longer available on this server and there is no forwarding address. Please remove all references to this resource.
To send this code use [G|gone] flag in rewrite rule:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/index.php.*
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ / [G,L]
RewriteCond %{REQUEST_URI} !^/index.php.*
RewriteRule ^(.*)$ /index.php/$1 [L]

Resources