Multiple url redirection in apache :mod_rewrite - apache

I am new to Apache and wish to redirect an existing url (1) to new url(2) and then to final url(3). The redirection from url (1) to url(2) is existing redirect rule defined and we cannot by pass it. Due to this we cannot jump form jump directly from url (1) to url(3). Below is the exsitng code:
for eg:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/portal-web-payment/(.*)$ /portal-web/$1 [R]
RewriteRule ^/portal-web/abc/portal/payment/web/mbbQuickRecharge/(.*)$ - [L]
</IfModule>
I wish to insert below rule into this existing one, which is not working.
RewriteRule ^/(.*)(portal-web/abc/portal/payment/web/mbbQuickRecharge/PrepaidQuickRechargeController.jpf)(.*)$ http://yahoo.com/servlet/du/en/mobilebroadbandrecharge.html [L,R=302]
Below is the final Redirect url we wrote, which is not working.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/portal-web-payment/(.*)$ /portal-web/$1 [R]
RewriteRule ^/portal-web/abc/portal/payment/web/mbbQuickRecharge/(.*)$ - [L]
RewriteRule ^/(.*)(portal-web/abc/portal/payment/web/mbbQuickRecharge/PrepaidQuickRechargeController.jpf)(.*)$ http://yahoo.com/servlet/du/en/mobilebroadbandrecharge.html [L,R=302]
</IfModule>
If suppose the rule is not working is due to already existence of apache flag [L], we cannot place our url before this due to project constraints.
Kindly suggest a way.
Thanks in advance.
Update 1:
Hi, its in .conf file.
the servername is our local env : in.test.it:90
<IfModule mod_rewrite.c>
ServerName in.test.it:90
RewriteEngine On
RewriteRule ^/portal-web-payment/(.*)$ /portal-web/$1 [R]
RewriteRule ^/portal-web/abc/portal/payment/web/mbbQuickRecharge/(.*)$ - [L]
RewriteRule ^/(.*)(portal-web/abc/portal/payment/web/mbbQuickRecharge/PrepaidQuickRechargeController.jpf)(.*)$ http://yahoo.com/servlet/du/en/mobilebroadbandrecharge.html [L,R=302]
</IfModule>

Related

Why is mod_rewrite adding var/www/html to the resulting url

I am trying to make my service backward compatible, since I have moved the service to a new path internaly, I still want the users to access it with the old url as not all of them have knowledge of this change.
I can accomplish what I want if I add [R] flag at the end of my rewrite rule but it redirects the url on the client side, which I don't want.
My rewrite code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/old-path(.*)$ /new-path/$ [L]
</IfModule>
Although this rule results in the following url:
/var/www/html/new-path
Sample request looks something like:
https://host-name/old-path/param1/param2/param3/param4
and rewrite rule should just replace old-path with the new-path.
Can anyone give me some clues about what am I doing wrong? and how can I fix it?
Thanks in advance!
If your are aiming for 'hiding' the rewrite, try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/old-path/(.*)$ /new-path/$1 [P,L]
</IfModule>
And if you REALLY want to extract the hostname:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} (.*)
RewriteRule ^/old-path/(.*)$ http://%1/new-path/$1 [P,L]
</IfModule>
Since %1 references the first bracket of RewriteCond-line...
If you want customers to be rewritten to the new URI and see that:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/old-path/(.*)$ https://host-name/new-path/$1 [R=301,L]
</IfModule>
or in short, and without even using a rewrite:
RedirectMatch ^/old-path/(.*)$ /new-path/$1
To avoid 'https://host-name/' change your DocumentRoot to the parent-directory of 'old-path' and especially 'new-path'.

Htaccess not getting 'GET' variables even with QSA flags

I can't seem to get my 'GET' variables passing through with my htaccess code. Here it is below:
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,L]
RewriteRule ^/(.*)/(.*)/(.*)$ v.php?shareid=$1&filename=$2 [QSA, L]
</IfModule>
I have v.php in the root, not in any folder and I am trying to achieve the following URL:
http://localhost/v/ubgvfmrsazwxoyp/Screen%20Shot%202015-11-05%20at%2020.51.45.png
where ubgvfmrsazwxoyp is the shareid and Screen%20Shot%202015-11-05%20at%2020.51.45.png the file name.
None are getting registered when page is loaded.
Note: I also want to keep .php extensions hidden.
Any ideas?
RewriteEngine On
RewriteBase /
RewriteRule ^v/([^/]+)/(.*)/?$ v.php?shareid=$1&filename=$2 [L]
You were previously getting v as the shareid and ubgvfmrsazwxoyp as the filename.
NOTE: The above code is specifically for an .htaccess file inside the server root directory. If the rewrite rules are going in the vhost config or the server config file, you'll need to use:
RewriteRule ^/v/([^/]+)/(.*)/?$ v.php?shareid=$1&filename=$2 [L]

.htaccess hide question mark from address

What am I doing wrong?
I have a link :
Link Name
I am need to make an address from:
eshop.mydomain.com/norma/something1/something2/something3/
this adress:
eshop.mydomain.com/norma-something1-something2-something3.html
the only thing I can do is
eshop.mydomain.com/?norma-something1-something2-something3.html
I need to hide the question mark, but I do not have any idea how :(
Here is my .htaccess
#php_value memory_limit 256M
RewriteEngine On
RewriteBase /
RewriteRule /index.php / [R=301]
RewriteRule ^norma/([^/]+)/([^/]+)/([\d\.]+)$ %{DOCUMENT_ROOT}/index.php?$1-$2&-$3.html [L,QSA]
ErrorDocument 404 /doc/e404/
You're adding that question mark in your rewrite rule:
# right here -----------------------------------------------------------v
RewriteRule ^norma/([^/]+)/([^/]+)/([\d\.]+)$ %{DOCUMENT_ROOT}/index.php?$1-$2&-$3.html [L,QSA]
But I suspect it may be a couple of things that's causing this redirect to happen when it really should be a rewrite internally on the server side. Try changing your rules to this:
RewriteEngine On
RewriteBase /
# this L is important ------------v
RewriteRule ^/?index.php / [R=301,L]
RewriteRule ^norma/([^/]+)/([^/]+)/([\d\.]+)$ /index.php?$1-$2&-$3.html [L,QSA]

.htaccess being overpowered by file structure in apache2

I have a .htaccess file and am using mod rewrite to redirect /tags to /tags/. When the directory contains a file called tags.php, it doesn't work, but when I remove that file it does. What setting do I need to change to make the .htaccess file overpower the file structure? I'm running Apache2 on Ubuntu 11.10 with PHP.
RewriteEngine On
RewriteBase /
RewriteRule ^tags/([a-zA-Z0-9-_.\ ]+)$ tags/$1/ [R]
RewriteRule ^tags/([a-zA-Z0-9-_.\ ]+)/([0-9]*)?$ tag.php?tag=$1&page=$2
RewriteRule ^tags$ tags/ [R]
RewriteRule ^tags/$ tags.php
Try adding an L to your tags rule to stop further processing i.e.
RewriteRule ^tags$ tags/ [R,L]
Your rules are in the wrong order, and you've forgotten the [L] to stop processing the rewrite rules. Try:
RewriteEngine On
RewriteBase /
RewriteRule ^tags$ tags/ [R, L]
RewriteRule ^tags/$ tags.php [QSA]
RewriteRule ^tags/([a-zA-Z0-9-_.\ ]+)$ tags/$1/ [R,L]
RewriteRule ^tags/([a-zA-Z0-9-_.\ ]+)/([0-9]*)?$ tag.php?tag=$1&page=$2 [L]
Disable Multiviews in the server configuration.

Redirect using .htaccess

I want to redirect all user page requests to a page on the same domain.
For example, I have an "under construction, BRB" page that I want all users to see when they try to access ANY page on the site.
I tried using this:
Redirect 302 / http://www.domain.com/index2.php
What that does is try to apply the redirect to the index2.php page as well and it gets stuck in a loop where the user then sees this until the browser stops.
http://www.domain.com/index2.phpindex2.phpindex2.phpindex2.php etc., etc,
Any idea on how to write that rule to except that page?
You have to exclude the file you want to redirect to. Here’s an example with mod_rewrite:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule !^index2\.php$ /index2.php [L,R=302]
</IfModule>
To prevent endless looping (index2.php <--> index2.php):
Only redirect If URL to be redirected does NOT contain the string 'index2.php'
RewriteCond %{QUERY_STRING} !index2.php
RewriteRule ^(.*)$ /index2.php [L,R=301]
You could use mod_rewrite
<IfModule mod_rewrite.c>
RewriteCond {REQUEST_URI} !=/index2.php
RewriteEngine on
RewriteRule ^.*$ /index2.php
# End .HTACCESS
</IfModule>
I'd be more inclined to use the slightly different
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/.*$ /index2.php [R=307,L]
</IfModule>
This will let you return a moved temporarily status for the redirect.
Omitting this set of flags means that mod_rewrite will return a 302 Found status by default.
HTH
cheers,