I have been developing web application using php framework - codeigniter at apache server.
For example, My server address has three address - 10...*, www.test.com, test.com
In codeigniter framework, I set the base url in config.php
$config['base_url'] = 'http://www.test.com/';
so I want that clients' requests will be redirect the www.test.com address.
That is,
http://test.com/ =======> http://www.test.com/
http://10.*.*.*/ =======> http://www.test.com/
In documentation, it talk me I have to use .htaccess file.
But, I don't know about it.
How to redirect the several address to one address?
In your htaccess file for the site you can use the following
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.test\.com$ [NC]
RewriteRule ^(.*)$ http://www.test.com/$1 [L,R=301]
</IfModule>
In case you haven't used mod-rewrite before, it needs to be enabled on your server. The .htaccess file needs to go in your document root folder, usually public or public_html.
Here are some instructions for enabling modrewrite on apache under Ubuntu. If you let me know what server or hosting you are using I may be able to point you to some better instructions. Note: You will probably find it's already enabled for you!
https://www.digitalocean.com/community/tutorials/how-to-set-up-mod_rewrite-for-apache-on-ubuntu-14-04
That should do it.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^10\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
</IfModule>
1st condition of each block means: host is not empty
2nd condition of 1st block means: host doesn't match www.*
2nd condition on 2nd block means: host matches an ip address starting with 10, it would match invalid ip aswell such as. 10.256..
Then redirecting (or rewriting) the request where 301 means permanent and L means stop rewriting.
For this to work, you'll need mod_rewrite enabled in apache.
Related
I have a PHP app on Heroku with an SSL certificate for the www version of the domain name. I need all requests (to both www and non-www) to go to via https, and I have added .htaccess to that affect. However, there are still circumstances where it's possible for a user to access the http version and I don't understand why.
Here is my .htaccess:
RewriteEngine on
RewriteCond %{HTTPS}::%{HTTP_HOST} ^off::(?:www\.)?(.+)$
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R]
My understanding is that this should force all users to access via https://www, but that doesn't always happen. For example, Google sometimes provides search results without the https and the links open insecure http instead.
Any ideas about what I'm doing wrong?
first redirect to the same host-name on :443, then redirect to www.. ordinary www. is just an alias in DNS, while most use the shorter non-www hostname for websites. you might have to extend the certificate, because it requires both host-names explicitly added, unless it's wild-carded.
# rewrite to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
if you want to rewrite all to www. (or whatever the certificate says), just add another rule below. at first access, the non-SSL rule [L] is the last step, at the next access the SSL rule [L] is the last step, of the rewrite.
# rewrite to www.
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
also see this answer here, concerning robots.txt with enforced SSL.
when it is "still possible to use HTTP" ...maybe consider another location for the .htaccess file - or create directories per host-name, which just redirect.
Try the following rules and let me know if it works or not these rule will use https request instead of http or www and non-www version. The following rule will now redirect the user to the something like this.
https://www.example.com/
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
Hope this will help to achieve what you wanted
I am deploying a PHP web application into a linux server for my client.
Their linux webserver allows wildcard subdomain, the server have SSL on.
I have set up the virtualhost for the wildcard subdomain and no problem occurs,
the problem is when i tried to use http:// instead of https:// on a wildcard subdomain, it links to other URL, it links to the clients own application.
I tried asking them about their server setting that redirects the non-https and they said they have no idea.
I tried modifying the .htaccess to redirect any http request to https but it does not work too, I suspected that there must be a missing server setting that caused this.
I have little knowledge on linux web server, and am really confused where should I even begin to search for this kind of problem, I've tried google for days and have not been able to find any solution.
here is my .htaccess setting, i put it on the root application.
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
the first rule is to delete index.php from the link, it works perfectly fine.
the second one is to redirect from http to https.
Any kind of answer or insight are very appreciated
I have a website that I have been working on for a while. All this time I have been editing the live website files on the web server for my development. I finally realized that it was stupid to edit the live site. My "quick" solution is to copy everything from example.com to beta.example.com. I will edit the beta subdomain and when I have a feature to release, I will copy and replace the existing code on the example.com site. Great.
That would be great if I could get it to work. I'm having troubles with my .htaccess files for both domains and URL rewriting.
For my example.com site, I use a .htaccess like so: (there are only a handful of pages on my site)
RewriteEngine On
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301]
RewriteCond %{REQUEST_URI} ^/recent
RewriteRule ^ recent.php [NC,L]
RewriteCond %{REQUEST_URI} ^/about
RewriteRule ^ about.php [NC,L]
RewriteCond %{REQUEST_URI} ^/category/(.*)/(.*)
RewriteRule ^ category.php?id=%1&name=%2 [NC,L]
RewriteCond %{REQUEST_URI} ^/category/(.*)
RewriteRule ^ category.php?id=%1 [NC,L]
The problem with the beta.example.com site is that it always redirects to just regular example.com. It removes the beta subdomain.
What is the proper .htaccess code to get subdomain sites separate from non-subdomains?
You have to configure settings for beta.example.com on your server. Setting like DNS in the domain account and if you are using windows(IIS)/linux(Apache) server then configure it same way as you have configured example.com.
If you are using shared hosting then you must have CPanel for your website. then create new directory through that and point the beta.exapmle.com to that directory.
I have a PHP website that I own. I want to temporarily disable the website while I am making my changes (there are a lot to do). I tried to rename the index.php file but the end-user can always navigate to a page by typing the URL ( or if he had bookmarked the page ). Is there a way I can disable the whole website temporarily?
Edit : This accepted answers work for an apache web server. What I am using presently is IIS6 ( and not IIS7 where the same rewrite can be done in web.config file ). Is there a way around for this problem in IIS6?
You can use .htaccess file to redirect to maintenance page:
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
RewriteCond %{REQUEST_URI} !^/maintenance\.html$
RewriteRule ^(.*)$ http://domain.com/maintenance.html [R=307,L]
Some useful links:
http://www.shellhacks.com/en/Redirect-Site-to-Maintenance-Page-using-Apache-and-HTAccess
http://perishablepress.com/htaccess-redirect-maintenance-page-site-updates/
http://wp-mix.com/maintenance-mode-htaccess/
While bartek beat me to an Apache mod_rewrite rule that would force the site to redirect all traffic to a maintenance/offline page, I wanted to post my variation on the idea which can allow a specific IP address to access the site:
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^123.45.67.890$
RewriteCond %{REMOTE_ADDR} !^127.0.0.1$
RewriteCond %{REMOTE_ADDR} !^::1$
RewriteCond %{REQUEST_URI} !^/offline.html$
RewriteRule ^(.*)$ http://your.great.website/offline.html [R=302,L]
Just a note that depending on your version of Apache, you might have to escape the . and : in the IP addresses like this:
RewriteCond %{REMOTE_ADDR} !^123\.45\.67\.890$
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1$
RewriteCond %{REMOTE_ADDR} !^\:\:1$
The idea is if the visitor is not coming from the IP address 123.45.67.890—or the localhost addresses of 127.0.0.1 (IPv4) or ::1 (IPv6)—and the URI requested is not /offline.html then redirect that person to http://your.great.website/offline.html.
Which means you should just replace 123.45.67.890 with the IP address you would be connecting from to allow you to have a window into the website while you perform work. But anyone else who is not 123.45.67.890? Well, they see the offline.html page.
I would also propose this solution.
RewriteEngine On
RewriteCond %{QUERY_STRING} !access=dbcaf771cc0c4e23a0fc895d0afa106f
RewriteCond %{REQUEST_URI} !^/maintenance.html$
RewriteRule ^(.*)$ /maintenance.html [R=302,L]
The idea is unless your request contains access=dbcaf771cc0c4e23a0fc895d0afa106f within the query string, you'll simply get redirected to the maintenance page.
I have both Windows & Linux Servers.(Domain manage in Linux Server ).
My Domain is
http://test.mydomain.com
Need to redirect to
http://192.97.XX.XX/Hello
I need All request that comes to http://test.mydomain.com Redirect it to http://192.97.XX.XX/Hello
Here i tried using htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.test.mydomain\.com$
RewriteRule ^/?$ "http\:\/\/192.97.XX.XX\Hello \/"
This will fail, because you are telling htaccess to look for www.test.mydomain.com, in addition to that, you're not escaping the . before test
If you change to this, you should be OK
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test\.mydomain\.com$
RewriteRule ^(.*)$ http://192.97.XX.XX/Hello [R=301,L]
This now says:
when the host starts with (that's what the ^ means) test.mydomain.com
redirect it to http://192.97.XX.XX/Hello, and make it a permanent redirect (the 301 part, so Google etc can update their indexes)
NB I've also changed the last line considerably
You can use this rule in document root of test domain:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?test\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://192.97.XX.XX/Hello/$1 [L,R]