block &sa=U&ei= requests in .htaccess? - .htaccess

I have a website that is getting a lot of requests like this:
http://site/folder/url.html&sa=U&ei=c9hNU7aVDOy(and more gibberish)
I've added this to my .htaccess:
RewriteRule ^(.*)&sa=U /$1 [L,NC,R=301]
But it doesn't work: the site still hands out 404 pages. (I've flushed the caches).
Help? Many thanks!

Usually, it would have been fair to use QUERY_STRING to match query string parameters.
But since you don't have leading ? it does not consider it as query string.
Your rule seems correct.
Anyway, it looks like it does not work as expected.
You can try the following alternative, which is working
RewriteRule ^(.*)&sa=U.*$ /$1 [L,NC,R=301]
EDIT: your rule is working the way you want, you only have to make your pattern more specific
RewriteRule ^([^&]*)&sa=U /$1 [L,NC,R=301]

If you want to make an rule base on the query string (= GET parameters), you have to use a RewriteCond, you can't access them through a RewriteRule. So :
RewriteCond %{QUERY_STRING} sa=U [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301]

Related

How to change my url

I have this url
http://example.com/profile.php?u=78
I wanted to changed it to be like this
http://example.com/78/
How would I do that?
Please Help
Thanks in advance
You can use this .htaccess in your root directory:
RewriteEngine on
RewriteRule ^(\d+)/?$ /profile.php?u=$1 [L]
RewriteCond %{QUERY_STRING} ^u=(\d+)$
RewriteRule ^profile.php$ /%1/? [R=302,L]
Change [R=302,L] to [R=301,L] when that work well
Try this to rewrite http://example.com/profile.php?u=78 into http://example.com/78/:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^profile.php$ /%2/
RewriteRule (.*) $1? [R=permanent]
Line-by-line explanation:
Turn on rewriting functionality
Specify the condition to make sure the query string is in the form of foo=bar for the following rules to apply.
Rewrite profile.php?foo=bar into /bar/?foo=bar
Remove all query strings that may still be appended to the new URL like /bar/?foo=bar/. This rule essentially substitute the entire path (.*) with itself ($1), and emptying the query string (?). [R=permanent] performs a permanent redirect (301).
Also, as previously commented, please ensure that http://example.com/78/ is a valid URL.

Issues Replacing underscores to hyphens with .htaccess

Although I already saw few posts about this thema, I am still having some problems to achieve what I want.
My old URL's were like:
http://myhostname.com/offer/1234_Nice_Offer_Cool_Whatever_HEllyeah.htm
And obviously I want to rewrite them to:
http://myhostname.com/offer/1234-nice-offer-cool-whatever-hellyeah.htm
This is the code I tried in .htaccess. This works fine to replace the underscores in URL's which they don't have ANY directory:
RewriteCond %{REQUEST_URI} \.htm$ [NC]
RewriteRule ^([^_]*)_+(.*)$ $1-$2 [E=underscores:Yes,N]
RewriteCond %{ENV:underscores} ^Yes$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
So, this paire of rules work fine with URL's like:
http://myhostname.com/1234_Some_Section_hellyeah.htm
But when I try the same code for URL's like:
http://myhostname.com/dir1/dir2/dir3/1234_Some_Offer_Or_Section.htm
Then, the server makes an infinite loop (I figure of the [N] flag...)
Basically, I would like to know in which way affects a per-directory URL to these rules and why am I getting this infinite loop. Thanks!
You don't need N flag. To replace underscore by hyphen recursively following code will work:
RewriteRule ^([^_]+)_(.+?\.htm)$ $1-$2 [L,NC,E=underscores:Yes]
RewriteCond %{ENV:REDIRECT_underscores} ^Yes$
RewriteRule ^([^_]+)$ /$1 [R=301,L]

.htaccess redirect - two URLs with similar paths

So here's what I have.
www.website.com/foo (pretty URL to use on marketing pieces)
www.website.com/foobar (URL that actually exists on site)
I can get www.website.com/foo working perfectly with this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /foo [NC]
RewriteRule ^ http://www.website.com/redirected/url-goes-here/ [L,R=301]
But that makes the www.website.com/foobar URL go there as well.
I'm sure this is a regex issue and I just don't know the correct symbol to get things working properly, but how can I make /foo redirect properly without effecting /foobar ?
Thanks.
Try this instead:
RewriteCond %{REQUEST_URI} ^/foo$ [NC]
RewriteRule ^ http://www.website.com/redirected/url-goes-here/ [L,R=301]
REQUEST_URI will get rid of the extra request headers that THE_REQUEST has. Then you can match the beginning and end of the requested URL with ^ and $.
You don't need the RewriteCond. Just be specific with the RewriteRule pattern
RewriteRule ^foo$ http://www.website.com/redirected/url-goes-here/ [L,R]
See more about regular expression.
Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.

htaccess rewritecond for a search result

my friends.
Please, I have an issue with ReWriteCond. I would like to create friendly search results. I just do it with ReWriteRule, but I would like to do the reverse path.
This is working pretty good:
RewriteRule ^search/([^/]*)$ /index.php?p=search&query=$1 [L]
But I would like to convert /index.php?p=search&query=$1 into search/query
Thanks.
Do redirect from "ugly" search form GET request to nice URL like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^p=search&query=(.*)$
RewriteRule ^index\.php$ /search/%1? [R=301,L]
Then you have to make sure, the "nice" URL will reach your script like this:
RewriteRule ^search/(.*)$ index.php?query=%1&p=search [L]
Notice the different order of arguments / it prevents infinite loop

Specific .htaccess redirect based on query_string

I need this logic to work:
I want rewrite this string for users to see
http://mysite.com/index.php?cl=mykeystring
to
http://mysite.com/otherkey/
http://mysite.com/index.php?myvar=test&cl=mykeystring&mysecondvar=morevalue
to
http://mysite.com/otherkey/myvar=test&mysecondvar=morevalue
But when http://mysite.com/otherkey/ is written, so load
http://mysite.com/index.php?cl=mykeystring, but no redirects will be done.
Is it possible? There are no possibility to change anything in codes, but only .htaccess
This logic is nearly realized by this code:
RewriteCond %{QUERY_STRING} ^(.*?)cl=mykeystring(.*?)$ [NC]
RewriteRule ^index.php$ /otherkey/%1%2? [R,L]
RewriteRule ^otherkey/(.*?)$ /index.php?cl=mykeystring&$1
but im getting some not needed amp symbols on first rewrite rule. any solutions?
I think you can do something like this:
RewriteCond %{QUERY_STRING} cl=mykeystring [NC]
RewriteRule ^index.php /otherkey/ [QSA]
Docs here: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

Resources