RewriteCond %{QUERY_STRING} - apache

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.

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]

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.

apache RewriteRule not working

I have this url http://www.example.com/Courses/get/38789/my-course, i added this rule to the .htaccess file
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/Courses/get
RewriteRule ^Courses/get/(.*)/(.*)$ course-$1-$2 [R=301,L]
but when i go to http://www.example.com/Courses/get/38789/my-course nothing happens, i stay on the same page, there is no redirect.
p.s the link is just an example not the actual link
A more efficient method would be to use the following:
RewriteRule ^Courses/get/(\d+)/([^/]+)/?$ /course-$1-$2 [R=301,L]
Now, keep in mind that this rule should come before any rules that may rewrite the request to, say, an index.php file. This would be naturally true if the code you posted in your question was all of your code. If not, please post your entire .htaccess file so we can be sure it is being placed in the right location.
Be sure the mod_rewrite is turned on, and the you have set AllowOverride All in your virtual host/Apache site configuration. If you're running on a shared production server, this would not apply to you.
Side note: Whilst it does work, you need not use RewriteEngine on twice - only once at the beginning of your file will suffice. You also do not need RewriteCond %{REQUEST_URI} ^/Courses/get - it is essentially redundant as you are already using an expression to test against the request.

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.

CakePHP redirect to external source under same domain

I've developed (using CakePHP) and deployed a web site on an Apache server (not managed by me) which provides a use statistics service under http://www.domain.com/stats. How do I tell the routes.php (or where should it be done) to not try to link the '/stats' with a StatsController but instead show the page provided by Apache?
Typically you put files in webroot when you want them to be accessible outside of cake. However, it sounds like this is not possible in your case. You could try adding a condition to the .htaccess file in the cake root directory.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/stats
RewriteRule ^$ app/webroot/ [L]
RewriteCond %{REQUEST_URI} !^/stats
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
I haven't tested this but the idea is to prevent redirects if the path starts with /stats which will avoid cake.
See:
http://book.cakephp.org/2.0/en/installation/advanced-installation.html
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html