Rewrite parameter url to normal url but it should get original url parameters after redirect - apache

Original URL:
https://www.example.com/all_users/user.php?id=1&subtype=p
Expected URL:
https://www.example.com/user/john-doe.html
I tried using following rule, it redirects to expected URL but I can't use parameters (id and subtype) with new URL.
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id\=1&subtype\=p$
RewriteRule ^all_users/user\.php$ https://www.example.com/user/john-doe.html? [R=301,L]
How can I use parameter values with new URL.
Example:
After redirect, new URL will be https://www.example.com/user/john-doe.html
Then I should able to get original parameters value with new URL.
$id = $_GET['id'];
$type = $_GET['subtype'];

With your shown samples and attempts, please try following htaccess rules.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect here.
RewriteCond %{THE_REQUEST} \s/all_users/user\.php\?id=\d+&subtype=p\s [NC]
RewriteRule ^ user/john-doe.html? [R=301,L]
##Internal rewrite from here..
RewriteRule ^user/john-doe\.html/?$ all_users/user.php?id=1&subtype=p [QSA,NC,L]

Related

Redirecting without query

I want to set redirection form ^mysite/ to www.mynewsite.example.
The problem is I don't want this direction when is www.mysite.example?test=test.
Redirection should works when client goes by www.mysite.example.
I tried to use RewriteRule
^mysite.xhtml$ www.mynewsite.example [L,R=301,NC]
But it still doesn't work
RewriteRule ^mysite.xhtml$ www.mynewsite.example [L,R=301,NC]```
I don't want this direction when my URL is www.mysite.example?test=test
You can use the following rule to stop the url redirection when your root url contains ?test=test querystring
RewriteEngine on
#if not ?test=test querystring
RewriteCond %{QUERY_STRING} !^test=test$ [NC]
#then Redirect the root url to www.newdomain.example
RewriteRule ^/?$ http://www.newdomain.example/ [L,R]
You can replace test=test with test=.+ in the RewriteCond above if it's a dynamic value.
^/?$ matches your root URI ie. / . If you want to redirect the entire site then replace it with ^.*$ .

Rewrite URL removing all after domain name

i need rewrite all URL of specific domain to other exact URL without parameters, all URL old.domain.com/* to new.domain.com
I have set this rule into .htaccess
RewriteCond %{HTTP_HOST} ^old.domain.com [OR]
RewriteCond %{HTTP_HOST} ^www.old.domain.com
RewriteRule ^(.*)$ https://new.domain.it [R=permanent,L]
so, if i go to
old.domain.com/foo or
old.domain.com/foo/bar or
old.domain.com/foo/bar/index.php
it work perfectly, but if i go to old.domain.com/index.php?goofy, it redirect to new.domain.com/?goofy, i can redirect always to new.domain.com?
When the url is old.domain.com/index.php?goofy Mod-rewrite appends ?goofy to its destination url. This is because mod-rewrite appends query string (Url part after the ? character) to the redirected url .
If you are on Apache 2.4 you can use QSD Query string Discard flag to remove old query string from new url.
RewriteRule ^(.*)$ https://new.domain.it [R=permanent,L,QSD]
If your apache version is below 2.4 ,simply add an empty question mark ? at the end of the target url to remove old query string.
RewriteRule ^(.*)$ https://new.domain.it/? [R=permanent,L]

VERY simple 301 htaccess redirect not working?

Very simple URL redirect from:
http://xxxx.net/index.php?k=Nomor-Bangkok&page=0
to:
http://xxxx.net/Nomor-Bangkok/online-0
When I try this (Old URL to new URL):
Redirect 301 /index.php?k=Nomor-Bangkok&page=0 /Nomor-Bangkok/online-0
I get a 200 header response without redirect.
But when I turn it around (New URL to old URL):
Redirect 301 /Nomor-Bangkok/online-0 /index.php?k=Nomor-Bangkok&page=0
Then I get a 301 redirect from new to old URL. But that's not what I want.
What am I doing wrong?
From the docs: "mod_alias is designed to handle simple URL manipulation tasks. For more complicated tasks such as manipulating the query string, use the tools provided by mod_rewrite."
So, for your exact example, you can do:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^k=Nomor-Bangkok&page=0$ [NC]
RewriteRule index.php /Nomor-Bangkok/online-0? [NC,L,R]
or even, as a more general rule if that is what you need:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^k=([a-zA-Z0-9-]+)&page=([0-9]+)$ [NC]
RewriteRule index.php /%1/online-%2? [NC,L,R]
which would redirect /index.php?k=AnyString&page=AnyNumber to /AnyString/online-AnyNumber

Redirect URL (including port) with params using .htaccess

I am trying to redirect from
http://www.example.com:81/my/api/search?query=test
to
http://www.example.com:81/my/php/api.php?query=test
using
RewriteCond %{QUERY_STRING} ^query=(.*)$
RewriteRule ^api/search(.*)$ /php/api.php?%1 [L]
However, it doesn't work for me. Additionaly for testing htaccess rules I use http://htaccess.madewithlove.be/.
Possibly even better would be to check if request starts from ^api and it's GET type (I guess using RewriteCond %{REQUEST_METHOD}) then redirect to /my/php/api.php?[query params here]
Anyone can point me into right direction?
If your .htaccess is located in /my/ directory then you can use:
RewriteEngine On
RewriteBase /my/
RewriteCond %{QUERY_STRING} ^query=.
RewriteRule ^api/search/?$ php/api.php [L]
QUERY_STRING will be automatically carried over to target URL.

How to use htaccess Rewrite rule for just part of a url

I am trying to create a rewrite rule in my httacess file for part of the url. I want to rewrite the url if the request contains a specific string. For example change the url only if contains /members/
So
mydomain.com/members/
mydomain.com/members/activity/ros1...
mydomain.com/members/ay/bd...
ALL above should change to another url because matches /members/ string in the url
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET /members/(.*)
RewriteRule ^(.*)$ - [F,L]
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/members(.*)
RewriteRule ^(.*)$ - [F,L]
I have tried various combinations but does not seem to work. I'm sure I'm doing something really wrong as not on expert on this. Appreciate any pointers.
RewriteCond %{REQUEST_URI} ^/members
RewriteRule ^(.*)$ http://www.google.com/$1 [R, 301]
Would be something i would try to redirect a request for /members and send them to another domain. (with the same request uri). Further info on mod_rewrite can be found https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule
You are using the F flag in your rewrites. The flag is to send the Forbidden status back to client. If you want to redirect the user to another URL, you'll need to pass that URL as the second parameter to RewriteRule directive with the R flag:
RewriteEngine On
RewriteRule ^members\b http://some-other-website.com [R]