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

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'.

Related

.htaccess rule for redirecting to parent

I am trying to redirect one of my urls to the parent folder using .htaccess file. I have tried the following rule
RewriteRule ^test/(.+)$ /test/ [L,R=301]
found from htaccess wildcard redirect to parent folder but it is not working (logs show too many redirects).
I also tried the other rules below but none of them worked
RewriteBase /
RewriteCond %{REQUEST_URI} !=/test/
RewriteRule ^(.*) /test/ [END,NC]
or
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^test/(.+)$ /test/ [L,R=301]
The OS is ubuntu server. Any help or pointers is appreciated. Please let me know if I can furnish any other details to debug. Thanks
Following should work considering the parent directory is test
RewriteEngine On
RewriteRule ^(test/).* /$1 [R=301,L]
Add this to disable MultiViews:
Options -MultiViews
The Apache docs on mod_negotiation, describes what the Multiviews Option does, when enabled:
If the
server receives a request for /some/dir/foo and /some/dir/foo does not
exist, then the server reads the directory looking for all files named
foo.*, and effectively fakes up a type map which names all those
files, assigning them the same media types and content-encodings it
would have if the client had asked for one of them by name. It then
chooses the best match to the client's requirements, and returns that
document.
Use:
Options -MultiViews
RewriteEngine on
RewriteRule ^test/(.+)$ /test/ [NC,L,R=301]
In your specific folder
RewriteEngine On
RewriteRule ^rootFName(.*) /rFile.php [QSA,R=301,L]

Multiple url redirection in apache :mod_rewrite

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>

Redirect with RewriteRule apache

I want to make an internal redirect with this result:
My old link is like: http://www.example.com/en/some-other-things
My new link should be: http://www.example.com/some-other-things
Basically I want to 'remove' the /en/ and get all the rest of the url in the new url. I've tried this rules in the .htaccess without success:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule "^/en/(.+)" "/$1" [R=301,L]
</IfModule>
Make leading slash optional so that rule works both in Apache config as well in .htaccess files. Use .* instead of .+ to ensure you match example.com/en/ as well. Also there is no need to quote the pattern or target.
RewriteEngine On
RewriteRule ^/?en/(.*)$ /$1 [R=301,L,NC,NE]

apache htaccess rewrite with alias

We are changing our domain name and this is meant to work for stand alone applications. In Apache virtual host file the DocumentRoot is /var/www/website/html, not /var/www/example/html as in this block:
Alias /apps/dept /var/www/example/html/apps/dept
<Directory "/var/www/example/html/apps/dept/">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
I put an .htaccess file in /var/www/example/html/apps/dept directory as follows:
RewriteEngine On
RewriteBase /apps/dept/
RewriteCond %{HTTP_HOST} ^example.orgname.state.tx.us/apps/dept [NC]
RewriteRule ^(.*)$ http://example.orgname.texas.gov/apps/dept/$1 [L,R=301]
This seems to follow what is recommended here, http://httpd.apache.org/docs/current/mod/mod_rewrite.html and Apache : How to Use Rewrite Engine Inside Alias. I see no results. The new domain has a virutal host config in the VH file, also. This same basic rewrite works for our Drupal website which does not use an alias. What changes might be necessary to have the domain name rewritten with an appended application pathname? Is the RewriteBase incorrect?
Thx.
So you only want to redirect /apps/dept/, correct? This should work. Place it as an .htaccess or in the Apache config for example.orgname.state.tx.us and all should work as expected.
RewriteEngine on
RewriteRule ^/apps/dept/(.*)$ http://example.orgname.texas.gov/apps/dept/$1 [NC,L,R=301]
So now, any requests going to this URL
http://example.orgname.state.tx.us/apps/dept/
Will now go to this URL:
http://example.orgname.texas.gov/apps/dept/
And any request parameters to the right of the URL will be passed along as well.
EDIT Just reread what you wrote here:
I put an .htaccess file in /var/www/example/html/apps/dept directory
as follows.
The .htaccess I described above should be placed in /var/www/example/html/ and not in the /apps/dept subdirectory.
But if you want the same behavior from an .htaccess placed in /apps/dept then use this:
RewriteEngine on
RewriteRule ^(.*)$ http://example.orgname.texas.gov/apps/dept/$1 [NC,L,R=301]
This way any request made from /apps/dept will to to example.orgname.texas.gov/apps/dept/ including subdirectories of /apps/dept such as /apps/dept/test_app1, /apps/dept/test_app2 or /apps/dept/test_app3.
Or perhaps try this:
RewriteEngine on
RewriteRule (.*)$ http://example.orgname.texas.gov/apps/dept/$1 [NC,L,R=301]
Note I removed the ^ which would force the RewriteRule to match the beginning of the URL.
You cannot match Request URI in %{HTTP_HOST} variable, it matches only domain name. Chang your rule to:
RewriteEngine On
RewriteBase /apps/dept/
RewriteCond %{HTTP_HOST} ^example\.orgname\.state\.tx\.us$ [NC]
RewriteRule ^(.*)$ http://example.orgname.texas.gov/apps/dept/$1 [L,R=301]

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,