Apache 2.2 rewrite rule for / is not redirecting - mod-rewrite

I have config file in apache that should redirect / request to different url.
But i am getting 404 when i hit /
Request is going to backend server but it is not redirecting.
Config file is
#This is not getting invoked. Returning 404.
#Request is redirected to app2.adobecqms.net with out appending page1/page2.html
RedirectMatch ^/$ https://app1.abc.com/page1/page2.html
RewriteCond %{REQUEST_URI} ^/folder1/folder2
RewriteCond %{REQUEST_URI} !^/(.*)\.(json|html)
RewriteRule ^/(.*)$ http://app2.adobecqms.net/$1.html [P]
RewriteCond %{REQUEST_URI} ^/analytics.txt
RewriteRule ^/(.*)$ http://app2.adobecqms.net/page1/page2/page3/$1 [P]
#I also tried with below. But no luck
#This is returning 502
#RewriteRule ^/$ https://app1.abc.com/page1/page2.html [P]
Any idea?

#This is not getting invoked. Returning 404.
#Request is redirected to app2.adobecqms.net with out appending page1/page2.html
RedirectMatch ^/$ https://app1.abc.com/page1/page2.html
Because you are using a mod_alias RedirectMatch directive, which is processed after the other mod_rewrite (RewriteRule) directives despite the order of directives in the config file.
And/or you have ProxyPass directives that forward other requests? mod_rewrite is required to override this.
You need to use mod_rewrite RewriteRule here also in order to avoid the conflict. For example:
RewriteRule ^/$ https://app1.abc.com/page1/page2.html [R=302,L]
And this directive should be before the existing directives. (Although there shouldn't be a conflict in this case.) The L flag is required.
The P flag sends the request through mod_proxy - this isn't a "redirect".
The RedirectMatch directive you posted would default to a 302 (temporary) redirect, as I used here. However, if this is intended to be permanent then change to a 301, but only once you have confirmed that it works as intended (to avoid caching issues).

Related

.htaccess rewrite to index.php or index.html based on condition

I'm not so good with htaccess and tried to find an answer to my question but no luck so far.
So I have this .htaccess rewrite:
RewriteCond %{REQUEST_URI} /(api|nova|nova-api)
RewriteRule .* /index.php
Which works well.
The website is an Angular site where I have dynamic URLs which are routed by JS.
So if I open base domain: example.com works well because index.html is served.
But if I open a route like: example.com/example-route. It says 404.
Could you please help me how should I modify the .htaccess file?
RewriteCond %{REQUEST_URI} /(api|nova|nova-api)
RewriteRule .* /index.php
You would seem to just need to rewrite the request to index.html after your API rewrite to index.php. However, you should modify your existing rule to add the L flag and the regex that matches the request should be anchored (although the condition is not required at all since the URL check should be performed in the RewriteRule directive itself).
For example, try the following instead:
# "index.html" needs to take priority when requesting the root directory
DirectoryIndex index.html
# Abort early if request is already "index.html" or "index.php"
RewriteRule ^index\.(html|php)$ - [L]
# Rewrite certain requests to Laravel API
RewriteRule ^(api|nova|nova-api)($|/) index.php [L]
# Rewrite everything else to Angular
# (unless it already maps to a file or directory)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]
Since the "homepage" is already working OK, it would suggest DirectoryIndex is already set OK in the server config (and prioritising index.html), although explicitly setting this to just index.html (as above) is more optimal, if this is all that's required.

How to redirect httaccess

I tried to redirect but not working even I put the correct code from take it from web.
Redirect /index.php?page=8 /?page=8
Redirect /index.php?page=8 /?page=8
The mod_alias Redirect directive does not match against the query string, so the above directive will never match, so does nothing.
To remove the index.php (directory index) from the visible URL, you would need to use mod_rewrite at the top of your .htaccess file. For example:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php$ / [R=301,L]
The above will redirect a URL of the form /index.php?page=8 to /?page=8. Any query string present on the initial request is simply passed through to the target/substitution unaltered.
The condition that checks against the REDIRECT_STATUS env var ensures we don't get a redirect loop caused by mod_dir (or the Laravel front-controller) rewriting the request to index.php.
Clear your browser cache and test first with a 302 (temporary) redirect.
However, if you did only want to redirect the specific URL /index.php?page=8 (as stated in the question) to /?page=8 then you should write the rule like the following instead:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,7}\s/index\.php?page=8\sHTTP
RewriteRule ^index\.php$ / [R=301,L]
Your htaccess code should be.
RewriteEngine On
RewriteRule ^index.php?page=$1 /?page=$1 [R=301,NC,L]

Apache rewrite : how avoid to display url parameter when the url final slash is omitted?

In my .htacess of my domain, I must point subdomain to the 1rst GET parameter of the domain. The subdomain represents the language (for example en., fr, etc...).
In order to achieve this aim, here the rewrite code in the .htaccess :
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$ [NC]
RewriteRule ^(.*)$ $1?lang=%1 [NC,L]
I create a directory named test. This directory contains just index.html file.
So when you type in the url bar of a browser en.example.com/test/,
the rewrite code works.
But if you type en.example.com/test without the final slash, it redirects to en.example.com/test/?lang=en => it's a problem.
So have you an idea to correct that ?
Thank you in advance, cordially.
When specifying xx.example.com/test/, an internal redirect
occurs to xx.example.com/test/?lang=xx. The client never sees the ?lang=xx.
However, when specifying xx.example.com/test, where test is a directory,
mod_dir steps in and rewrites the URL to xx.example.com/test/, but in
such a way that the rewrite rule for the ?lang=xx redirect becomes public,
having the client see xx.example.com/test/?lang=xx as the URL - which is unwanted.
In order to keep the '?lang=' redirect local (hidden from the client)
place this in the .htaccess, BEFORE the original rewrite rules:
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ $1/ [R,L]
The condition checks whether the requested filename is a directory,
and the rule forces a client-side redirect [R], so that a client requesting xx.example.com/test will be redirected to xx.example.com/test/.
The key however is the [L], which makes this rule the Last, preventing the following rules from executing. Without this L flag, the entire redirect from xx.example.com/test to xx.example.com/test/?lang=xx becomes public.
After the client is forcefully redirected to the proper URL with a terminating /, the rewrite rules doing the internal redirect adding the lang GET parameter are executed as normal.
Here's the entire .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ $1/ [R,L]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$ [NC]
RewriteRule ^(.*)$ $1?lang=%1 [NC,L]
There is however another way to achieve this without using Apache config and internal redirect, and that is to examine $_SERVER['SERVER_NAME']:
<?php
list( $lang ) = explode('.', $_REQUEST['SERVER_NAME'] );

URL Rewrite to redirect a old host to a new host

I know nothing about Url Rewrites, but we have recently changed domain names from oldcompanyname.co.uk to newcompanyname.co.uk.
We have been told that to get oldcompanyname.co.uk to not show up in search results anymore, we need to redirect the old url to the new one.
I have installed ISAPI_Rewrite 3 but have not been successful in getting the re-direction to work.
On our development servers I've tried the following code without success to redirect any request to http://tm-devtest2/tmintranet to http://tm-devtest2/tmintranet.
# Helicon ISAPI_Rewrite configuration file
# Version 3.1.0.94
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.*internet*$
RewriteRule ^$ http://tm-devtest2/tmintranet/ [R,L]
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !newcompanyname.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.newcompanyname.co.uk/$1 [L,R=301]
The RewriteCond is only needed in case the same instance will serve both domains.
L means "Last", i.e. stop rewriting
NC means "no case"
R means Redirect
301 means "Permanently Moved"

mod_rewrite "400 Bad Request" problem

I can't seem to get past a Bad Request error while setting up mod_rewrite. I've been trying for a while, so here's what I have.
The url I'm trying to access is:
gordons.local/brewCalc
The page I'd like to see is
gordons.local/index.php?page=brewCalc
Here's my rewrite rule:
RewriteEngine on
RewriteLog /var/www/gordons.com/logs/rewrite.log
RewriteRule ([^/]+)/?$ index.php?page=$1 [L]
I've used a regex tool, and this tool, but no matter what I end up with a page that says:
Bad Request
Your browser sent a request that this server could not understand.
Additionally, a 400 Bad Request error was encountered while trying to use an ErrorDocument to handle the request.
Apache/2.2.12 (Ubuntu) Server at gordons.local Port 80
Also, I'm not getting any information in my access, error or rewrite logs.
EDIT: My rewrite rules are in my vhost file. (/etc/apache2/sites-available/gordons.local)
In case anybody ever finds themselves here, my issue was a missing leading slash before the replacement.
RewriteRule ([^/]+)/?$ index.php?page=$1 [L]
Should have been
RewriteRule ([^/]+)/?$ /index.php?page=$1 [L]
Grrrr....
If you see Apache's error.log you would be able to see the actual error. Most likely you are trying to put above rules in .htaccess file and RewriteLog is not allowed in .htaccess file. Also your RewriteRule will redirect more than you intend. So if you comment out your RewriteLog and have your RewriteRule like this then it should work:
RewriteEngine On
RewriteBase /
# request is not for a file
RewriteCond %{REQUEST_FILENAME} !-f
# request is not for a directory
RewriteCond %{REQUEST_FILENAME} !-d
# forward to index.php
RewriteRule ^([^/]+)/?$ index.php?page=$1 [L,QSA,NC,NE]
NC - Ignore case comparison
NE - Do not encode RHS URI
QSA - Append existing Query String into new one
L - Mark it last rule

Resources