.htaccess rule for redirecting to parent - apache

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]

Related

RewriteCond %{QUERY_STRING}

Here's what I'm trying to accomplish:
http://example.com/real-estate/?group=rentals
needs to go to
http://example.com/real-estate/rentals
Here's what I have in my .htaccess that isn't working:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/real-estate/ [NC]
RewriteCond %{QUERY_STRING} ^group=rentals
RewriteRule (.*) http://example.com/real-estate/rentals/? [NC,R=301,L]
</IfModule>
This is very unusual... Typically people want the redirection the other way around....
Your code looks nearly fine, only some minor corrections. But those might be what you are missing:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/real-estate/?$ [NC]
RewriteCond %{QUERY_STRING} ^group=rentals$
RewriteRule ^ http://example.com/real-estate/rentals [R=301,L]
</IfModule>
For this to work the rules have to either be placed directly inside the http hosts configuration, or inside a .htaccess style file in the hosts document root with enabled interpretation of such files.
And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using .htaccess style files. Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).
Just use this in your .htaccess file:
RewriteEngine On
RewriteRule ^real-estate/([^/]*)$ /real-estate/?group=$1 [L]
It will leave you with the URL: http://example.com/real-estate/rentals. Make sure you clear your cache before you test this.

Shorten url with .htaccess

I do not know .htaccess so much. What i want to do is to shorten my url. I have searched on Google and found many solutions. Still none of them is working.For exmaple my url is "example.com/video?id=12345/filename". I want an url like "example.com/video/12345/filename". I have tried thisRewriteEngine on
RewriteRule ^video video?id=$1 [QSA]But it is not working. Please help.
Here is my .htaccess code
Options +FollowSymLinks -MultiViews -indexes
RewriteEngine On
RewriteBase /
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [L,R=301]
RewriteRule ^video/([0-9]+)/.+$ video?id=$1 [L,QSA]
RewriteRule ^video/([0-9]+)/.+$ video?id=$1 [L,QSA]
ErrorDocument 404 /index.php
I guess this is what you are looking for:
RewriteEngine on
RewriteRule ^video/([0-9]+)/(.+)$ video?id=$1/$2 [L,QSA]
I would however suggest that you simply drop the filename, you don't need it to deliver the file. Since you have some numerical ID you certainly have some form of database that stores the physical location of the referenced file. In that case the above would have to be changed to:
RewriteEngine on
RewriteRule ^video/([0-9]+)/ video?id=$1 [L,QSA]
And another, general hint: if possible you should always prefer to place such rules into the real host configuration of your http server. You should only fall back to .htaccess style files if that really is not possible for you, for example because you do not have access to the http server configuration. Reason is that .htaccess style files are notoriously error prone, a security risk, hard to debug and really slow the server down.

RewriteRule webroot to file-system path

Have checked out lots of topics here and there regarding the issue in question,
but no results yet.
So here is the issue.
Two projects:
proj: site.loc
proj2.0: newsite.loc
They are located in different places on the file system.
And i need to redirect from one project to another internally,
so url for site.loc is preserved.
E.g. requesting site.loc/hey/there i need apache to serve files form proj2.0.
First, i know that on the .htaccess level we cannot use RewriteRule to file-system path
(for security reasons).
Okay, an Alias is a workaround.
Say i add an Alias to virtual host as following:
Alias /newsite /some/path/to/proj2.0
Then if i'll add the rule to proj's .htaccess:
RewriteRule ^hey/there /newsite
This will work.
But, the webroot does not work:
RewriteRule ^$ /newsite
Is it i'm doing something wrong or there is some quirk about the webroot ?
Thanks in advance
You should not rewrite to your root, but suppress it. Here is how I have done it in my project to suppress the www
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>
There should be a way to make it work with your /newsite in the same manner.

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]

.htaccess RewriteRule adds drive path to URL

I am using Zend Server CE (v.5.1.0) installed on C: on a Win7 machine. I have added one project to httpd.conf by adding:
Alias /project "D:\Homepages\project"
<Directory "D:\Homepages\project">
Options Indexes FollowSymLinks
AllowOverride all
Order allow,deny
Allow from all
</Directory>
My .htaccess file in the project directory contains the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^/\w*\.(css|js) [NC]
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Now to the problem; if I go to
http://localhost/project/index.php
everything seems to be working fine. I reach the index.php file and get my contents.
However, if I go to any other page that would trigger the RewriteRule, it seems to be adding the directory path. FireFox outputs the following Not Found message:
The requested URL /Homepages/project/index.php was not found on this server.
I tried to find a similar question/answer here, but failed. Any idea?
Ps. Me accepting of an answer might be delayed as I will be out for a while on an errand.
You need to set the RewriteBase directive; otherwise, mod_rewrite automatically, and by default, prepends the file path to the resulting rewrite rule.
From: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
When a substitution occurs for a new URL, this module has to re-inject the URL into the server processing. To be able to do this it needs to know what the corresponding URL-prefix or URL-base is. By default this prefix is the corresponding filepath itself. However, for most websites, URLs are NOT directly related to physical filename paths, so this assumption will often be wrong! Therefore, you can use the RewriteBase directive to specify the correct URL-prefix.
If your webserver's URLs are not directly related to physical file paths, you will need to use RewriteBase in every .htaccess file where you want to use RewriteRule directives.
Have your last line like this:
RewriteRule ^.*$ /index.php [NC,L]
However I think this is infinite loop so I would suggest this rule instead:
RewriteCond %{THE_REQUEST} !\s/index.php [NC]
RewriteCond %{REQUEST_URI} !^/index.php [NC]
RewriteRule . /index.php [L]
which prevents going to index.php if it is already /index.php.