Dynamic URL redirect - .htaccess

How to redirect my url - www.example.com/home.php?rid=888601032
to www.example.com/examplepage.php
i have tried several htaccess codes 7 Im new to htccess, I would appreciate if someone can give me the correct redirect code.

The redirect you need is incredibly simple.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(home\.php)?\?rid=888601032\ HTTP/
RewriteRule ^(home\.php)?$ http://www.example.com/examplepage.php? [R=301,L]
It redirects any root request with the RID within it.

The syntax is:
redirect accessed-file URL-to-go-to
There are 3 parts;
(1) the Redirect command,
(2) the location of the file/directory you want redirected, and
(3) the full URL of the location you want that request sent to.
These parts are separated by a single space and should be on one line.
For example, if you want to redirect users from oldfile.html in the www directory of your account, myaccount, to newpage.html, the syntax should be
redirect /~myaccount/oldfile.html http://www.indiana.edu/~myaccount/newpage.html

Put this in yout .htaccess file:
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} rid=888601032
RewriteRule ^home.php examplepage.php [R,L,QSA]
If you dont want the redirect to be visible, leave the [R] flag.
Hope this helps!

Wouldn't be better to use php to do this?
if ((int)$_GET['rid'] == 888601032) // or something like this
header('Location: examplepage.php');
It depends on what you want to do, but it would be more flexible if you want to redirect to a different page depending on the "rid" parameter, or if you want to do some operation (like setting a cookie) before redirecting the user.

Related

Redirect and keep the parameter in the url on .htaccess

It should probably be an easy setup, but without familiarity with the terms used, I can't come up with a solution. I tried several examples that I found on the internet.
I just wanted that when the user accessed the site via a url https://example.com?foo=bar he would be directed to https://www.example.com/new-nice-page?foo=bar
Changing the page and keeping the parameter.
The last thing I tested:
RewriteEngine on
RewriteRule ^https://www.example.com/new-nice-page?foo=$ example.com?foo=$1 [QSA]
You may use this redirect rule at the top of .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)foo= [NC]
RewriteRule ^/?$ /new-nice-page [L,R=301]
Note that query string is automatically carried forward to target URL.
Looking at this thread : simple .htaccess redirect : how to redirect with parameters?
you simply redirect , without worying about the parameters, because they are automatically passed :
Redirect permanent /jsn.php http://www.site2.com/jsn.php

Redirect all urls which contain certain parameters to another url which follows a certain pattern

Unfortunately I didn't get it solved by myself and need to ask for help. I want to redirect all urls which follow a certain pattern (in this case it contains "reviews/category"). These URLs supposed to be redirect to another url which is made up the first one:
http://[product-url]/reviews/category/[category-url]
supposed to be redirect to
http://[product-url].html
Furthermore it shouldn't matter if you call the url with or without www.
Example:
http://example.com/ford-blues/reviews/category/cars supposed to be redirect to http://example.com/ford-blues.html
Any help would be much appreciated.
Following code isn't working
RewriteEngine On
RewriteRule ^reviews/category/?$ $1\.html [R=301,L]
Try:
RedirectMatch 301 ^(.*)/reviews/category/ /$1.html
in the htaccess file in your document root.
Or using mod_rewrite:
RewriteEngine On
RewriteRule ^(.*)/reviews/category/ /$1.html [L,R=301]

General rulefor redirect in htaccess

I want to make some redirects in htaccess. I will explain in detail what I'm trying to do and at the end I will tell you my question, so if you don't want lots of explains just scroll down.
So for you to understand what I am doing, here is an example: if I enter www.mysite.com/oldpage it should redirect me to www.mysite.com/new page. For this I used:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
Redirect /oldpages http://www.mysite.com/newpages
The problem is that I want to use this for more than one page. So I want that:
mysite.com/oldpage/firstarticle
to redirect to
mysite.com/newpage/firstarticle
And the same with all articles. I want to redirect all like:
Redirect /oldpages http://www.mysite.com/newpages
Redirect /oldpages/firstarticle http://www.mysite.com/newpage/firstarticle
Redirect /oldpages/secondarticle http://www.mysite.com/newpage/secondarticle
But I have a conflict because Redirect /oldpage works on every case.
That's why (AND HERE IS WHAT I DON'T HOW TO DO!!!) I need to use a general rule like:
Redirect /oldpage/(*) http://mysite.com/newpage/(*)
Redirect /oldpage/(any numeric characters-don't need them)/(*) http://mysite.com/NEWSINGLEPAGE/(*)
Instead of (*) it should be any characters,but use them in the new link.
Does someone know how to make this Redirect for more than one page?
Thank you for your time!
!!!!
I DON'T KNOW IF IT'S POSSIBLE BUT IT WOULD BE NICE IF I COULD USE AN IF STATEMENT IN HTACCESS...SOMETHING LIKE:
if (adress is /redirect) Redirect to newpage.html;
else if (adress is /redirect/(some number)/(.*)) Redirect to NEWSINGLEPAGE/$1.html
else if (adress is /redirect/(.*)) Redirect to newpage/$1.html
Change
Redirect /oldpage/(*) http://mysite.com/newpage/(*)
To
RedirectMatch /oldpage/(*) http://mysite.com/newpage/$1
try this
RewriteRule ^oldpage/(.*)$ /newpage/$1 [R=301,L]

.htaccess - If/Else redirect depending on query string

Setting up an .htaccess seems to be difficult for this situation. For a dating site we have this scenario:
We assume john is logged in:
domain.com/john //this works no problem
domain.com/?askout=yes&when=later //this doesn't work, if john is logged in and pastes
this url, it automatically redirects to domain.com/john
instead, htaccess should see the ? and know it's
not a valid user. It should keep the entire query,
and redirect to /profiles.php?askout=yes&when=later
Here is the .htaccess:
RewriteRule ^(\w+)/?$ ./profiles.php?username=$1
Basically, I need to have .htaccess know that an immediate ? means not to use the above rewrite rule. Any idea how to accomplish this?
Try:
RewriteCond %{QUERY_STRING} askout=yes&when=later
RewriteRule ^ /profiles.php?askout=yes&when=later [L]
This rule needs to go before the rule you have redirecting to the ?username target.
You need a RewriteCond I think: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html, with which you can do all manner of checks

301 Dynamic Pages Redirect

Hi I tried various methods with modewriter in htaccess shown in other threads. I want to redirect my url from www.example.com/categories.php?category=1 to www.example.com/categories/science.
I would really appreciate an example too where I'm wrong. Thank You.
Assuming that it is just this one URL you want to redirect it would be like:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^category=1$ [NC]
RewriteRule ^categories.php$ www.example.com/categories/science/? [L,R=301]
This is in case you are using a .htaccess file, otherwise add a prepending slash:
RewriteRule ^/categories.php$ www.example.com/categories/science/? [L,R=301]
The querystring wil be discarded. Only put [R] if you want to show it to the user in the addressbar of the browser. Consider which status you want to serve, Google likes 301 if it's permanent.
Next you have to catch the incoming request with an index.php in the directory www.example.com/categories/science
good luck!

Resources