I have a URL such as http://www.domain.com/index.php?p=register. I want to redirect that to use HTTPS (SSL) with .htaccess, but only on this, and a couple of other pages (the login page, etc), but not the entire site. The URLs don't point to directories, but are used to dynamically include different files.
Can someone give me a pointer or an example of how to get a single page redirect to HTTPS please?
Thanks.
Not htaccess, but another way could be to use PHP to redirect:
<?php
$redirectlist = array('register','login','myaccount');
if (in_array($_GET['p'], $redirectlist) && strtolower($_SERVER['HTTPS']) != 'on') {
exit(header("location: https://{$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}"));
}
?>
The only reason I mention this is that, in some cases, this may be easier to maintain than a separate htaccess. You would need to put this in place in your PHP content before any text was outputted (see header()).
something like this should work:
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{QUERY_STRING} (^|&)p=(register|login|or|other|protected|page)($|&)
RewriteRule (.*) https://www.domain.com/index.php [R=301,QSA,L]
Some explanation:
Check if server port is different from 443 (standard for secure connections), to ensure we are going to redirect only non-secured connections
The query string (everything after ?) have to match pattern: include p variable with one value from pipe separated list
Redirect everything to secure domain, sending 301 response status, appending all query string and marking is as last rule, so any rules below this will not be parsed (since this is redirect, we don't want to take any other actions)
If you have option to follow the php method, I would recommend to follow that or with any other dynamic languages. You must avoid using htaccess since links to images, js and other contact on that page will be forced to be nonSSL and modern browsers would show a non-compliance sign which might look a whitewash over your SSL cost.
Related
Hello guys. I started coding my own "URL shortener". The basic idea is you use example.com/12345 to redirect to another URL. This "matching" is done by using .htaccess to redirect stuff towards a script that does (irrelevant for us now) stuff.
My .htaccess currently looks like this:
RedirectMatch 302 ^/\w{5}$ /redir.php
The redirect matches any string of exactly 5 and sends it toward my PHP script where the actual redirection to the expanded URL take place. The only problem is that I was unable to find a proper way of getting the original URL, the matched one into a variable.
As a sidenote the whole thing happens on a VPS set up by me with minimal knowledge, so if this problem can originate from a missing config ($_SERVER['HTTP_REFERER'] doesn't work), then expect my configs to not be 100% correct and by standards.
EDIT: changed from RedirectMatch to RewriteRule, still doesn't work.
RewriteRule ^\w{5}$ /redir.php [R,L]
you can use the following rule:
RewriteRule ^(\w{5})$ /redir.php?redir=$1 [R,L]
this will send the 5 letter string as querystring param redir. Which you can access in redir.php as:
$_GET['redir']
Edit: Or as #LawrenceCherone have suggested you can use $_SERVER['REQUEST_URI'] in redir.php. But for that you have to use NC flag in .htaccess instead, Like:
RewriteRule ^(\w{5})$ /redir.php [NC,L]
I'm working with a client who has a site with many subdomains representing different areas covered in his locksmith business. He picks up a lot of traffic from directory websites, and wants to use his domain only as the link on these websites. When someone clicks it, he wants them to be redirected based on a keyword in the referring URL.
For example, a referring Yell URL could be
yell.com/ucs/UcsSearchAction.do?keywords=locksmith&location=Watford%2C+Hertfordshire&scrambleSeed=1311994593
Client wants htaccess or something similar to pick out the keyword 'Watford' from that URL, and redirect to watford.hisbusiness.com accordingly.
This isn't something I've done before and I'm baffled. Research found no clues.
You can check HTTP_REFERER to grab information from the referring URL.
RewriteEngine on
RewriteCond %{HTTP_REFERER} yell\.com/.*\?.*\&location\=(\w+)\%2C\+(\w+)
RewriteRule ^$ http://${lower:%1}.hisbusiness.com/ [R=302,L]
The ${lower:$1} is used to make Watford lowercase. In order for this to work, you'll need to add the following to your httpd.conf or virtual host configuration file:
RewriteMap lower int:tolower
Note: The rule in place above is designed for the domain root (hisbusiness.com) only - that is to say that a request to hisbusiness.com/something won't trigger the redirect. If you'd like it to check for the URI as well, use the following rule instead:
RewriteRule ^(.*)$ http://${lower:%1}.hisbusiness.com/$1 [R=302,L]
To make the redirect permanent and cached by browsers/search-engines, change 302 to 301.
Use Header on PHP using your required conditions:
if(condition 1){
header("Location: http://mywebsite1.com");
}
if(condition 2){
header("Location: http://mywebsite2.com");
}
else{
header("Location: http://mywebsite3.com");
}
You can use [stristr][1] on the if condition.
I am trying to use mod_rewrite to redirect users keeping the multiple query string and creating a redirect page
For Example,
If user opens
http://localhost/url/url/http://www.google.com/contacts/?user=abc&stackoverflow=great&google=facebook
then he is taken to
http://localhost/url/url.php?redirect=http://www.google.com/contacts/?user=abc&stackoverflow=great&google=facebook
There is secondary problem that URL should be encoded and then redirected! If URL is not encoded then the string (&stackoverflow=great)would be not a part of 'redirect' string of url.php
I tried many solutions then came for stackoverflow! I tried the following code in following file
http://localhost/url/.htaccess
RewriteRule ^url/([^/])$ url.php?redirect=$1 [QSA,L]
but the result is localhost/url/url.php?redirect=http only
Your setup won't work with the unencoded inner url, so an 'answer' can only have temporary character. But this might be a starting point:
RewriteEngine on
RewriteRule ^/url/url/(.*)$ /url/url.php?redirect=$1 [L,QSA]
I wonder however if that fragment /url/url is really intended (the two 'url's in there).
Note that the exact rule content also depends on where you want to define that rule. The syntax is different whether you use the central server configuration (referred) or .htaccess style files (as second choice and more complex).
Try this
RewriteEngine on
Redirect ^url/url/(.*)$ url/url.php?redirect=$1
The basic redirect systax,
redirect accessed-file URL-to-go-to
My company uses Xerox Docushare for document management. We are consolidating 2 docushare servers into one. Assuming users have a lot of docushare pages bookmarked in their browser, is it possible to place a php file in the root folder which will receive all these requests and perform a redirect.
For example
http://old-server/docushare/dsweb/View/Collection-xxxx
would get redirected to
http://new-server/docushare/dsweb/View/Collection-yyyy
The collection-xxxx to collection-yyyy would probably come from a file we intend to generate as part of the conversion.
I did take a look at
http://php.net/manual/en/function.header.php
but that is on a url level whereas i am looking to convert all requests on the older path.
Thanks.
By my opinion, the simplest way is to put .htaccess file. In the root of your document root
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} ^old-server
RewriteRule http://old-server/docushare/dsweb/View/(.*)$ http://new-server/docushare/dsweb/View/$1 [R=301,L]
For more inspiration check this page
The PHP way
In front controller or whatever is hitten as first by web server, will be condition, using $_SERVER variable, similar to this
if($_SERVER['SERVER_NAME'] == 'old-server')
{
$redirectionPath = str_replace('http://old-server/docushare/dsweb/View/', 'http://new-server/docushare/dsweb/View/', $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
header(sprintf('Location: %s', $redirectionPath), 301);
}
This is the ugly way and you should not use it unless you have no other choice. Not to mention my blind written code ;)
I don't know exactly in what situation you are, but i think the .htaccess file solution solves issue you are experiencing
I have a script that on certain events blocks someones ip via a htaccess deny from.
This is the code:
file_put_contents('.htaccess', 'deny from ' . $_SERVER['REMOTE_ADDR'] . "\n", FILE_APPEND);
Yet what i want is instead od deny from this user can i have it redirect the user to another page.
I can do this via:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_HOST} !^123\.45\.67\.89
RewriteRule \.html$ /alternate_page.html [R=302,L]
But is it possible to do it without opening the htaccess and appending to it. Instead use the file_put_contents function
Thanks A Lot.
You don't need to use .htaccess for redirecting users to other pages, just use the location header based on what you get from $_SERVER['REMOTE_ADDR']:
header("Location: http://anothersite.com")
and don't forget this (from the PHP manual):
Remember that header() must be called
before any actual output is sent,
either by normal HTML tags, blank
lines in a file, or from PHP. It is a
very common error to read code with
include(), or require(), functions, or
another file access function, and have
spaces or empty lines that are output
before header() is called.
I would recommend storing IP addresses you do not want to allow in a database. This makes administration and if you want stats, they can be added easily as well (ie. How many times has this user tried to access the site after being denied, etd). You could even send certain IPs to different location if you wanted.
Store the IP addresses in a database table
When a user comes to the page check to see if their IP is in the table
If so increment stats counter (if desired) and redirect user to new location
Or continue loading the current page
Use php's Header function Header('Location: [url here]'); to redirect them and remember it must be before any other content is sent to the page.
What you can do is have your .htaccess file like this:
Options +FollowSymLinks
RewriteEngine on
# ADD RULES HERE
RewriteCond x x # This is a dummy condition that is always true
RewriteRule \.html$ /aternate_page.html [R=302,L]
Then you can have your replacement work like this:
<?php
file_put_contents(".htaccess",str_replace("# ADD RULES HERE","# ADD RULES HERE\nRewriteCond %{REMOTE_HOST} ^".$_SERVER['REMOTE_ADDR']."$",file_get_contents(".htaccess"));
?>
HTH
(Edit: This doesn't seem quite right... So may need a little editing. Hope the general idea helps, though.)
The proper way would be to use a gatekeeper function, which checks the IP, and forwards the request to the required page on need.
<?php
header('Location: http://www.yoursite.com/new_page.php');
?>
You should call this before any output is sent to the browser, including any HTML tags or white space. The common way is to have a 'forward' function wrapping the header command, and do access control first, before sending any content.