301 redirects with regular expressions - .htaccess

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]

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

.htaccess rewriting GET

I've never needed to use a .htaccess before and I'm fairly new to coding I'm trying to get localhost/index.php?id=123456789 to be passed as localhost/123456789/ but I just cant get the HTaccess right, i've tried everything I could find from prevoius posts here, haha!
My current HTACCESS looks like this, however it doesnt do what I want.
RewriteEngine On
RewriteRule ^id/(\d+)$ /index.php?id=$1 [NC,QSA,L]
You can do it with mod_rewrite in .htaccess however I'm not sure from your question which direction you want it to be passed to.
If you want people to type the php script in the URL bar you want:
RewriteEngine on
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^index.php$ %1/
Or if you want people to enter the "pretty" version but for your server to load the PHP script you need:
RewriteEngine on
RewriteRule ^([0-9]+)/$ index.php?id=$1&%{QUERY_STRING}
For both ways and 301 redirect to pretty URL:
RewriteEngine on
RewriteRule ^([0-9]+)/$ index.php?id=$1&%{QUERY_STRING} [L]
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^index.php$ %1/ [L,R=301]

Using 301 redirect inside htaccess file

I used to have a link like:
www.domain.com/index.php?id=IdParameter&place=true
But I don't use "place=true" anymore.
I want all those links to look like:
www.domain.com/index.php?id=IdParameter
WITHOUT the "&place=true" part.
How can I redirect links including "place=true" to themselves, without that last part?
thanks
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.+?)&place=true$ [NC]
RewriteRule ^index\.php$ %{REQUEST_URI}?%1 [L,R]

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

modrewrite - rewrite to new domain from subdirectory

Damn you modrewrite
I have a website hosted at a url like:
http://mydomain/mocks/thesite/
Now I want to move it to a new domain
http://thesitesdomain.com/
My htaccess looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.thesitesdomain\.com
RewriteRule (.*) http://www.thesitesdomain.com/$1 [R=301,L]
Now this works fine as long as there is something after /mocks/thesite/. eg: http://mydomain/mocks/thesite/index.html redirects to http://www.thesitesdomain.com/index.php.
However the problem is that:
http://mydomain/mocks/thesite/ redirects to http://thesitesdomain.com/mocks/thesite/. Any idea why? How to stop this?
The .htaccess file is in the root of /mocks/thesite/ (if that helps)
Thank you
You should try to use the variable REQUEST_URI you might have a little more success with that. It should be the request uri and file name. To
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.thesitesdomain\.com
RewriteRule .* http://www.thesitesdomain.com/%{REQUEST_URI} [R=301,L]
I can't remember but to also redirect with the query string (get variables) I think you need to add it like this.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.thesitesdomain\.com
RewriteRule .* http://www.thesitesdomain.com/%{REQUEST_URI}?%{QUERY_STRING} [R=301,L]
Been a while since really doing a domain redirect....
BTW this is a good read on htacces configuration:
http://corz.org/serv/tricks/htaccess2.php

Resources