Rewrite rule based on query string - .htaccess

I'm trying to redirect to the base url (www.example.com) requests that have a particular query string (when option is com_estateagent).
I've tried the following syntax:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^option=com_estateagent$
RewriteRule .* index.php [R=301, L]
But it gets ignored.
Any suggestions?
EDIT
The url that I want to change is something like this:
http://www.example.com/subdirectory/index.php?option=com_estateagent...

I don't think you need the RewriteCond directive, wouldn't something like this work?
RewriteRule ^/.*option=com_estateagent /index.php[R=301,L]

This works
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} option=com_estateagent
RewriteRule .* subdirectory/index.php? [R=301, L]
Notice, the ? after index.php. That's important if you want to drop Query Parameters on redirect.

Related

url rewriting and moving of questionmark

I've tried all manor of rules and just can work this one out. If there someone who can solve this?
I need to turn this
http://localhost/search/?terms=foobar
into this
http://localhost/index.php?m=search&terms=foobar
Where "foobar" is really ([^/]*), ie can be anything, it's a search string.
The initial URL has a "?" in it due to the GET method of HTML forms. Although it would be easier without that "?" it's not something that can be done?
I've tried the following with no luck.
#RewriteRule ^search/\?terms\=([^/]*)$ index.php?m=search&terms=$1 [L]
Thanks.
For query string you need to use %{QUERY_STRING}:
RewriteCond %{QUERY_STRING} ^terms=(.*)$ [NC]
RewriteRule ^search/?$ index.php?m=search&terms=%1 [NC,L]
You cannot match QUERY_STRING in RewriteRule. Have your code like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^(search)/?$ index.php?m=$1 [L,NC,QSA]
Because of QSA flag %{QUERY_STRING} will automatically be carried over to new URL.

dynamic to static url AND removing the dynamic

I want to change alle dynamic url's to static ones,
but after rewriting the dynamic url's are still responding/available.
What did I do =>
I found this Tool for SEO:
http://www.webconfs.com/url-rewriting-tool.php
I entered this:
.../filmdetails.html?var=ich_einfach_unverbesserlich_ii
Then I put into my .htaccess this:
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^filmdetails/(.*)/$ filmdetails.html?var=$1
Works, but now I got a problem: This URL is still available and should not be: .../filmdetails.html?var=ich_einfach_unverbesserlich_ii
How do I get rid of the dynamic url's?
Your rule only rewrites the nicer looking URL to the one with a query string. Rules only work from a "pattern" -> "target" way, the mapping won't magically work the other way. You'll have to create a separate rule in order to redirect the browser:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /filmdetails\.html\?var=([^&\ ]+)&?([^\ ]*)
RewriteRule ^ /filmdetails/%2/?%3 [L,R=301]
Try:
Options +FollowSymLinks
RewriteEngine On
#set this to path to your filmdetails.html file (from the document root)
RewriteBase /
#checking if redirect already happened
RewriteCond %{ENV:REDIRECT_PASSED} !^$
RewriteRule $ - [L]
#Your rewrite rule
RewriteRule ^filmdetails/(.*)/$ filmdetails.html?var=$1 [L,E=PASSED:1]
#redirecting from filmdetails.html with query string ?var=something
RewriteCond %{QUERY_STRING} ^var=(.+)$ [NC]
RewriteRule ^filmdetails.html$ filmdetails/%1/? [R]
filmdetails.html?var=something will be redirected to filmdetails/something

.htaccess RewriteRule is not working

RewriteRule new/$ /search.php?category=1
RewriteRule new/\?(.+)$ /search.php?category=1&$1
I'm trying to do something like this, if the following address link is accessed,
http://onlineshop.com/new/
http://onlineshop.com/new/?price_max=30
then it will open this link,
http://onlineshop.com/new/search.php?category=1
http://onlineshop.com/new/search.php?category=1&price_max=30
Unfortunately it is not working this way.
A RewriteRule won't naturally catch query string parameters, you must use this kind of .htaccess :
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^new/$ /search.php?category=1&%1
You can just simply redirect from /new to /search.php with QSA flag and Apache will append the existing query string. Something like this will work for you:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^new/?$ /search.php?category=1 [L,QSA,NC]

Remove variable from base URL with htaccess

I've been trying to rewrite a URL such as
www.somesite.com/?x=372
into a url
www.somesite.com/
My current code does not seem to work
RewriteEngine On
RewriteCond %{QUERY_STRING} x=(.*)
RewriteRule http://www.somesite.com/ [R=301,L]
I've looked up countless ways of trying to do it with htaccess and still no success.
If you simply want to redirect a client to remove the query string (everything after the ? in the URL), then you can try this:
RewriteEngine On
RewriteCond %{QUERY_STRING} x=(.*)
RewriteRule ^ http://www.somesite.com/? [R=301,L]
You've got most of it right, it seems, but your rule needs to have a match, and your target (the http://www.somesite.com/) needs a ? at the end so that any query string before the rewrite won't get appended.
In Apache 2.4 or newer you can use the QSD query string discard flag:
RewriteEngine On
RewriteCond %{QUERY_STRING} x=(.*)
RewriteRule .* http://www.somesite.com/ [R=301,L,QSD]

htaccess - rewrite rule to get end of url

I have a url which ends with a certain variable string, and was erroneously generated and indexed unfortunately.
Example:
http://domain.com/anything-in-between/?var=xyz-abc-abc-abc
How can I redirect to main site (kill it), by detecting 'abc-abc-abc' using htaccess?
Why wouldn't this work and what would be the best solution:
RewriteCond %{REQUEST_URI} abc-abc-abc
RewriteRule .* index.php
You want to use the query string as claesv suggests but you need to then kill the query string
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} \bvar=.*?abc-abc-abc$
RewriteRule ^ index.php? [L]
This will do it silently (i.e. in the server as an internal redirect and not involving the browser). You can't use 301s reliably to trim query strings.
Something along the lines of:
RewriteCond %{QUERY_STRING} ^var=.*abc-abc-abc$
RewriteRule ^.*$ http://domain.com/ [R=301,L]

Resources