About url rewrite with query string - .htaccess

Please help, how to make this url
(in my request i use get function)
iplookup.php?lookup_ip=66.249.66.1
to looks like this?
/ip/66.249.66.1
with
RewriteCond %{QUERY_STRING} lookup_ip=
RewriteRule ^ip/(.*)$ iplookup.php?lookup_ip=$1 [QSA]
But unsuccessful :(

Get rid of the condition. The RewriteCond %{QUERY_STRING} lookup_ip= condition is only true if lookup_ip= is in the query string, and obviously the URI /ip/66.249.66.1 doesn't have one. Your rule should just look like this:
RewriteRule ^ip/(.*)$ /iplookup.php?lookup_ip=$1 [L,QSA]
Then when you request http://yourdomain.com/ip/12.34.56.78, the browser's URL address bar remains unchanged while you get served the content at /iplookup.php?lookup_ip=12.34.56.78. You just need to make sure all of your links look like http://yourdomain.com/ip/12.34.56.78.

Related

Rewrite URL'S with a question mark “?”

I have a few url that have appeared in google search console with "?" and under scores "_"in the URL. I have tried many ways to redirect them but i have failed. I believe it has some thing to do with using the %{QUERY_STRING}
this is the URL that i need redirecting from
repairs-blog?journal_blog_tag=iPhone
to
blog?journal_blog_tag=iPhone
Maybe someone could write the solution?
When a query string is involved, you use RewriteCond with %{QUERY_STRING}, e.g.
RewriteCond %{QUERY_STRING} journal_blog_tag=
and then rewrite with RewriteRule
RewriteRule ^repairs-blog$ /blog [L]
Because you don't change the query string, there's nothing more to do.
When you want to rewrite this URL, no matter what query string is given, you can also omit the RewriteCond and just use the RewriteRule.
If you want to redirect instead of rewrite, e.g. change the URL in the client, you add the R flag
RewriteRule ^repairs-blog$ /blog [R,L]

HTaccess dynamic URL to static rewrite that works both ways without loop

I have looked through stack overflow and can't find the exact answer I am looking for so here goes.
I have a URL
http://holtplantandmachinery.co.uk/cat/wheel-loaders which redirects to http://holtplantandmachinery.co.uk/category.php?name=wheel-loaders
However I need it to work the other way around when you enter the dynamic URL it rewrites to the static (folder looking) URL above and vice versa. Essentially whatever one is called it should rewrite to the static one. Now I know I need to setup a rewrite that works the opposite way to the one above and then have a condition to stop an infinite loop but I can't seem to get anything to work please help.
When someone types http://holtplantandmachinery.co.uk/category.php?name=wheel-loaders into the browser's URL address bar, the request sent to the holtplantandmachinery.co.uk server looks something like:
GET /category.php?name=wheel-loaders HTTP/1.1
Host: holtplantandmachinery
some other headers
etc
You can use the %{THE_REQUEST} var to match against the first line:
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ category\.php\?name=([^&\ ]+)
RewriteRule ^ /cat/%2? [L,R=301]
This will redirect the request to http://holtplantandmachinery.co.uk/cat/wheel-loaders and that URL will show up in the browser's URL address bar. The reason why you use %{THE_REQUEST} instead of %{QUERY_STRING} is because when your other rule that rewrites the nicer looking URL to the one with the query string, it'll look and the %{QUERY_STRING} would match, the rule would get applied and cause a redirect loop. Alternatively, you could match against the query string and use a:
RewriteCond %{ENV:REDIRECT_STATUS} !200
To ensure the URI wasn't from a previously rewritten internal redirect.
Then you'd have your internal rewrite rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?cat/(.*)$ /category.php?name=$1 [L]

.htaccess 301 redirect that excludes a subpage

My rule looks like this:
RewriteRule ^page-parent.*$ http://www.domain.com/new-page/?%{QUERY_STRING} [R=301,L]
My issue is that there is a page /page-parent/thanks that I don't want to be redirect.
I am also passing the query string along so that any ?gclid= string will go with.
I can't for the life of me figure out how to exclude a single sub page or all sub pages, which would work too.
Any help is appreciated.
You can check with a RewriteCond if the requested page is the page you want to exclude.
RewriteCond %{REQUEST_URI} !^/page-parent/thanks
RewriteRule ^page-parent.*$ http://www.domain.com/new-page/ [R=301,L,QSA]
Make sure that you clean the browser cache when retrying since 301-redirects get cached.
Note: You don't need to append the query string manually.
You can use mod_rewrite to create a condition:
RewriteCond %{REQUEST_URI} !^/page-parent/thanks
RewriteRule ^page-parent.*$ http://www.domain.com/new-page/ [R=301,L]
Note that you can leave out the ?%{QUERY_STRING} from your target, because query strings are appended to the target by default unless you've added new params via a ?.

Not pass full query string to redirected url in htaccess

I am creating a htaccess file, I currently have a long url looking like this:
domain.com/file.aspx?id=1000&AE=value1&PL=value2
I want to redirect it to look like:
seconddomain.com/directory/subdirectory/1000
(i.e. just use the value of the ID key passed).
This is what I'm using:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=([0-9]+).*$
RewriteRule ^file.aspx$ http://seconddomain.com/directory/subdirectory/%1 [R=301,L]
However, what I end up getting is:
http://seconddomain.com/directory/subdirectory/1000?id=1000&AE=value1&PL=value2
i.e. the value of ID gets put in the correct spot -- however, the full query string is appended. I do not have a QSA flag.
What am I doing wrong?
Thank you in advance.
I think the query string is automatically appended if you don't set one yourself, try sticking a ? at the end of the redirect path to override that and see if that helps
EDIT
Just to clarify your code would look like this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=([0-9]+).*$
RewriteRule ^file.aspx$ http://seconddomain.com/directory/subdirectory/%1? [R=301,L]

how to redirect request based wether a query string value is present?

I've searched and tried many examples but none seem to work for me. I need to redirect a request to a specific url depending on if the original request had a certain item in it's query string.
e.g.
www.mydomain.com/test.html?username=foo&password=bar
So I want to redirect this only is the username variable is present in the query string to
www.mydomain.com/home.html?username=foo&password=bar
But also, the page could be any page, .e.g test.html, home.html, contact.php
So if username variable is detected in query string, then the redirect will happen only.
thanks for your help.
I tried this but it did not work:
RewriteRule ^username(.*)$ test.html?username=$1 [L,QSA]
You can use QUERY_STRING to accomplish this.
RewriteCond %{QUERY_STRING} username
RewriteCond %{REQUEST_URI} !^/home.html
RewriteRule .* /home.html/ [L,QSA]

Resources