htaccess rewritecond for a search result - .htaccess

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

Related

Redirect htaccess stuggles

I would like to make a rewrite rule for my website but I cannot seem to get the proper code in order to make this work.
The current URL of my website is looking like http://www.mohanadarafe.io/JSON/json.html
I want it to be: http://www.mohanadarafe.io/json
I have tried the following code but it does not seem to work:
RewriteEngine On
RewriteRule ^\.html$ /json [L]
Any idea how to fix this?
You have it reversed. Have it like this:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /JSON/json\.html [NC]
RewriteRule ^ /json [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^json/?$ JSON/json.html [L,NC]
Then you can have your URL as: http://www.mohanadarafe.io/json

RewriteRule to rewrite /folder/subfolder/file to /?f=folder/subfolder/file?

Pretty straightforward - just wondering how do I rewrite this:
/folder/subfolder/file
/?f=folder/subfolder/file
And the number of folders/subfolders might vary, so what I really want is anything after the / in that path (that does not already have ?f= in it) redirected to the path with ?f= added in front of it.
If I add this
RewriteRule ^(.*)$ ?f=$1 [L]
it works just fine but doesn't recognize the existence of ?f= in the URL already, and breaks that existing link with ?f= in there.
Any ideas?
You can use:
RewriteCond %{QUERY_STRING} !(?:^|&)f= [NC]
RewriteRule ^(.*)$ ?f=$1 [L,QSA]
Which does not do the rewriting if the f querystring is already there.
With [QSA] you keep the other possible querystring values.
Try this :
RewriteEngine On
RewriteRule ^(.[^\?]*)$ /?f=$1 [L]

block &sa=U&ei= requests in .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]

301 redirects with regular expressions

I'm having some trouble using the right code for my .htaccess file.
What I'm trying to accomplish is this:
We have a QR code generator which generates random url'Ss like this:
http://mydomain.com?APP-V2/7091c104-32a8-4680-9c07-a75c3ee61d7a/00
I need to redirect all these url's to the homepage, http://mydomain.com.
How to I write the wildcard in my htaccess file? Basically everything after mydomain.com?APP-V2/ should be redirected.
Any help much appreciated!
Basically everything after mydomain.com?APP-V2/ should be redirected.
If you want:
http://mydomain.com/?APP-V2/7091c104-32a8-4680-9c07-a75c3ee61d7a/00
to be redirected to:
http://mydomain.com/
Then you just get rid of the query string (e.g. ?APP-V2/7091c104-32a8-4680-9c07-a75c3ee61d7a/00):
RewriteEngine On
RewriteCond %{QUERY_STRING} ^APP-V2/
RewriteRule ^$ /? [L]
But if you want everything after the ?APP-V2/, you need this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^APP-V2/(.*)$
RewriteRule ^$ /%1? [L]

.htaccess - How do you do this? http://www.example.com/domain.com

i want to use somthing like http://www.example.com/domain.com instead of http://www.example.com/index.php?url=domain.com.
how can I do this using .htaccess?
update: i finally figured it out. :)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1
http://www.pagerankcheckertool.com/facebook.com
RewriteEngine on
RewriteRule ^(.+)$ index.php?url=$1
Depending on your needs, it may not be such a good idea to have everything rewritten as per your example, e.g. even a www.example.com/index.html would be rewritten to www.example.com/index.php?url=index.html so i'd recommend you use an initial sub folder or something in the url to seperate your rewritten urls from anything else.. i.e. www.example.com/urls/domain.com
To accomplish that you could setup a rewrite rule.. (assuming you have mod_rewrite active)
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^urls/(.+) /index.php?url=$1 [NC]
that basically means any url that begins with urls/ and has one or more characters following.. the brackets around the .+ will 'group' that element and allow you use it again with the $1
Hope that solves what you want to accomplish!

Resources