htaccess - rewrite rule to get end of url - .htaccess

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]

Related

How do I create dynamic friendly urls for a query string?

So currently I have URLs that looks like this:
http://localhost/?v=register
http://localhost/?v=profile&u=thatgerhard
I want the above to look like this:
http://localhost/register/ (without trailing /)
http://localhost/profile/thatgerhard/ (without trailing /)
I spent a ton of time trying to get this to work even though it seems like it should be a simple fix.
This is what I have atm:
RewriteBase /
RewriteEngine On
RewriteRule ^((.)*)$ /?v=$1&p=$
I would ideally like this to be dynamic so that if you do ?v=foo it will automatically do /foo/ without adding "foo" to the htaccess file.
Any help or direction will be greatly appreciated. :)
You should use RewriteCond Directive and RewriteCond backreferences. The %1 backreference matches the query string parameter v, the %2 backreference matches the query string parameter u. The RewriteRule redirects the request permanently and discard the query string entirely.
RewriteCond %{QUERY_STRING} ^v=([^&]*)$ [NC]
RewriteRule ^/?$ /%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^v=([^&]*)&u=([^&]*)$ [NC]
RewriteRule ^/?$ /%1/%2/? [R=301,L]

htaccess rewrite ?p= (I don't understand the code)

I think I have read everything I can about htaccess rewrites and I still can't make heads or tails of whats going on. I re made a website for work and all is well except the last designer did some crazy php stuff and all the urls he used have ?=p(pagename) I want to rewite those to (pagename).php then redirect them to with a 301 I am able to get the 301 redirects works just can't figure out how to rewrite the ?p=(pagename) to (pagename).php
You want to be matching against the actual requests, then internally rewrite it back to the query string:
RewriteEngine On
# 301 redirect to php file
RewriteCond %{THE_REQUEST} \ /\?p=([^&\ ]+)&?([^\ ]*)
RewriteRule ^ /%1.php?%2 [L,R=301]
# internally rewrite to the query string
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.php$ /?p=$1 [L,QSA]
You need to check the QUERY_STRING and then apply the rewrite rule
RewriteCond %{QUERY_STRING} ^p=(.*)$
RewriteRule ^(.*)$ http://mydomain.com/%1.php [R=301,L]
the ^p=(.*)$ checks for a query string that only has the one variable p=pagename, you will have to modify it if there will be any other variables in the query string it like p=pagename&id=15 etc

Rewrite rule based on query string

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.

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 redirect loop and regex issues

I have a simple htaccess redirect to send visitors from website.com/promotions to website.com/go/promotions. If I use the code below I get a redirect loop error:
RewriteEngine on
RewriteCond %{REQUEST_URL} !^go$
RewriteRule promotion/index.html go/promotion/index.html [R,L,NC]
The RewriteCond is supposed to prevent the loop by not redirecting if the request url contains "go" but it doesn't appear to be working.
In addition I'm trying to set up a regex so that promotion/anypage.html will be redirected to go/promotion/anypage.html. I tried the following code but it wouldn't match anything:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^go$
RewriteRule ^promotion/(.*)$ go/promotion/$1 [R,L,NC]
Does anybody have an clues as to why either of these blocks isn't working properly ?
Cheers
%{REQUEST_URL} isn't a var you can match against, you're probably thinking of %{REQUEST_URI}, but it starts with a leading slash so ^go will never match anything.
You just need a single rule:
RewriteEngine On
RewriteRule ^/?promotion/(.*)$ /go/promotion/$1 [R,L]
Or you can use mod_alias:
Redirect /promotion/ /go/promotion/
You're checking if the entire URL is just "go" on it's own, which it's not:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/?go/
RewriteRule promotion/index.html go/promotion/index.html [R,L,NC]

Resources