localhost directs to www.domains.tld after adding .htaccess in codeigniter folder - codeigniter

I just started using Codeigniter to develop a simple static website using a local server (MAMP). Initially my local address to access my homepage was http://localhost/index.php/home. Even a simple localhost would redirect to my homepage. I wanted to remove the 'index.php' from the URL and hence I copy pasted the .htaccess code which I found online. The code looked like the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
### Canonicalize codeigniter URLs
# If your default controller is something other than
# "welcome" you should probably change this
RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
# Enforce www
# If you have subdomains, you can add them to
# the list using the "|" (OR) regex operator
#RewriteCond %{HTTP_HOST} !^(www|subdomain) [NC]
#RewriteRule ^(.*)$ http://www.domain.tld/$1 [L,R=301]
# Enforce NO www
RewriteCond %{HTTP_HOST} ^www [NC]
RewriteRule ^(.*)$ http://localhost/$1 [L,R=301]
###
# Removes access to the system folder by users.
# Additionally this will allow you to create a System.php controller,
# previously this would not have been possible.
# 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
</IfModule>
Initially I copy-pasted the code without changing it at all (which was foolish on my part) So the 'localhost' in the code above was initially 'www.domains.tld'. When I then ran localhost on my browser, it directed to www.domains.tld'. I noticed the blunder and changed it to what it is above and localhost still directs to 'www.domains.tld' I deleted the .htaccess file to reverse the effect but it still does the same thing.
I also changed my root folder for localhost but whatever I do localhost points to 'domains.tld'. When I type 127.0.0.1 on the address bar of my browser, it directs correctly to my website. I have spent hours reading up on the reason for this behavior but am unsuccessful to find a solution.
Any help will be greatly appreciated.
Thanks,
H.

Clear your DNS cache and restart your browser. Certain browsers cache redirects with DNS so that you end up at what it thinks is the correct site more quickly.
Firefox is particularly annoying with this, so I disable its internal DNS caching every time I install. In about:config, create the following two integer type settings (NOTE: you MUST create these, they do not exist by default):
network.dnsCacheEntries set to 0
network.dnsCacheExpiration set to 0
In Chrome, you would need to turn off DNS prefetching under your Privacy settings.
The reason they do this is to make the internet "seem" faster to casual browsers. For developers, it can be quite a hindrance.

Related

How do I strip out ?_escaped_fragment_= using .htaccess

Google discovered that I'm allowing end users to navigate my content using ajax loading, and is loading my pages as a user client rather than requesting them as new page loads. So instead of trying to index www.mysite.com/page, it's requesting www.mysite.com/?_escaped_fragment_=/page
Which is not at all what I want it to do. My snapshots are served at the same URL as the ajax-loaded content. The site is not using queries, it's not supporting them and I don't want to build that support. This means that all the pages look broken to google which of course is unfortunate!
Currently all page requests are redirected server side using .htaccess sending requests to the index.php file which in turn compiles the html doc on the server before serving to the client. The site serves perfectly valid and unique html documents for all pages. But google insists on doing it the ajax way and adding the query which always returns a broken page.
I'm not a .htaccess expert, but it seems to me that the easiest way to solve this would be to rewrite the request, remove the ?_escaped_fragment_=/ bit and permanently redirect any such requests to what currently works which is to load the pages using their correct url's.
Anyone know how I would go about doing that? Below is the current redirect part of my .htaccess file which needs to be amended with the _escaped_fragment_ stripping code:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
#if trailing / remove it with a permanent redirect
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
#if missing www. add it with a permanent redirect
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
#requests for index.php never rewritten
RewriteRule ^index\.php$ - [L]
#if file or directory are missing, route to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This is how I rewrote it so that all ?_escaped_fragment_=/XXXXX requests got redirected to /XXXXX without the query
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=(.*)$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%1? [L,R=301]
This makes www.domain.com/?_escaped_fragment_=/somepage redirect (permanently) to www.domain.com/somepage
...which is just what I wanted.

301 URL Forwarding with HTACCESS or PHP

Just curious if anyone can help me on this HTACCESS issue.
I have these OLD URLS that need to get forwarded properly.
Previous structure
domain.com/Canada/Accounting
domain.com/Canada/Trades
domain.com/Canada/Sales
Proper structure
CATEGORY - /jobs/accounting-jobs
LOCATION - /jobs/jobs-kelowna
TOGETHER - /jobs/accounting-jobs-kelowna
Domain Structure
domain.com/jobs/[category]-jobs-[location]
Is this possible, either by HTACCES or PHP...just don't want these 404'ed pages.
I have 86+ to do, if there is a good way to forward these.
This is what I have, but i'm unable to successfully forward the bad-urls properly.
OLD
/browse
/Toronto/
/Canada/Administrative
/Vancouver/
/Canada/Trades
/Calgary/
/Canada/Hospitality
This is my HTACCESS right now.
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
#
# Trailing slash check
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]
#
# PAGES
RewriteRule ^add-job/?$ /add-job.php [L]
RewriteRule ^jobs/?$ /results.php [L]
RewriteRule ^sitemap/?$ /sitemap.php [L]
#
# SEARCH
# CATEGORY - accounting-jobs
# LOCATION - jobs-kelowna
# TOGETHER - accounting-jobs-kelowna
RewriteRule ^jobs/([a-zA-Z0-9_-]+)/([0-9]+)?$ results.php?whatwhere=$1&page=$2
RewriteRule ^jobs/([a-zA-Z0-9_-]+)/([0-9]+)/?$ results.php?whatwhere=$1&page=$2
To 301 redirect your pages you can do something like:
RewriteEngine On
RewriteBase /
RewriteRule ^(\w+)/(\w+)$ /jobs/$2-jobs-$1 [R=301,L]
This only addresses the urls from your previous structure (the combinations, you have not shown any previous urls with just location or category) but note that Canada will stay Canada, it does not become canada. You can change everything to lower case using rewrite as well.
You also have to take care that you don't rewrite any of the current urls but without more information, this should do it.
Edit: For the location-only urls you could use a rule like:
RewriteRule ^(\w+)/$ /jobs/jobs-$1 [R=301,L]
Again, you need to look out that your rewrite rule does not interfere with your current urls. If that is the case, you would need to redirect every old url manually.
For lower-case new urls, you should search SO, there are some questions with good answers about converting a mized-case variable to lower-case.
If you have mod_rewrite, you can add these lines to your .htaccess file:
RewriteEngine on
RewriteRule ^Canada/Accounting$ /jobs/accounting-jobs [R,L]
However, it's not clear from your question exactly what you want mapped. Are the 3 previous URLs supposed to redirect to the 3 new ones? They don't seem to be equivalent.

Using mod_rewrite to redirect old urls with Codeigniter

I'm redeveloping a website using the codeigniter framework.
When we go live, we want to ensure a few of the old URLs will be redirected to the appropriate pages on the new site.
So I put what I thought would be the correct rules into the existing htaccess file, above the other rules that CodeIgniter applies.
However, they are not taking affect. Can anyone suggest what I'm missing here?
# pickup links pointing to the old site structure
RewriteRule ^(faq|contact)\.php$ /info/ [R=301]
RewriteRule ^registration\.php$ /register/ [R=301]
RewriteRule ^update_details\.php$ /change/ [R=301]
# Removes access to the system folder by users.
RewriteCond %{REQUEST_URI} ^_system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
# This snippet prevents user access to the application folder
# Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^myapp.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
# This snippet re-routes everything through index.php, unless
# it's being sent to resources
RewriteCond $1 !^(index\.php|resources)
RewriteRule ^(.*)$ index.php/$1
Try adding a [L]ast flag to your R=301 flag => [L,R=301] that makes sure no other rules are applied, and, just to be sure, try to redirect to a complete URL and, to be even more sure you haven't deleted anything, add RewriteEngine On to the top and set the RewriteBase.
Make your first rows look like
RewriteEngine On
RewriteBase /
RewriteRule ^(faq|contact)\.php$ http://www.YOURDOMAIN.XYZ/info/ [L,R=301]
and check if the URL in your browser changes when you call for instance the faq page.

codeigniter, force ssl on a particular page

I'm having hard time trying to force https (ssl) to particular page on codeiginiter framework. I tried many ways, but none worked. The only way worked for me is changing the $config['base_url'] site link to begin with https instead of http. The result was that all links were set to ssl (the entire site), which is none sense because I don't need to use the SSL everywhere. I used some php code in the login page, ut that made some troubles so I gave it up.
I want to know whether this is a good method how to do that, any idea?
Thanks,
You need add Condition on .htaccess to make use SSL port work only for selected urls.
Here is example, how to do
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} controller/function
RewriteRule ^(.*)$ https://www.yourdomain.com/controller/function[R=301,L]
RewriteCond %{SERVER_PORT} 443
RewriteCond %{REQUEST_URI} controller/function
RewriteRule ^(.*)$ https://www.yourdomain.com/controller/function[R=301,L]
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
P.S : Your base_url must be set to “/” in your config file.
For more information check http://codeigniter.com/wiki/SSL_Handling

301 Redirect Dynamic HOST includes DOCUMENT_ROOT

Based on what I read on Apache I used the following example they provided to do a 301 Redirect on all my web sites.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
This is not quite working as they said it would. If I try www.domain.com it works. If I try domain.com I get www.domain.com//home/www/public_html/www.domain.com
Looks like it wants to include the DOCUMENT_ROOT in the redirect. Am I better off to create an individual .htaccess for each web site?
What is faster to run - Apache or HTACCESS?
Try this instead. Make sure you include the RewriteBase /
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
If you still get the old result, your previous 301 redirect is probably cached, retest in Private (Incognito) Browsing Mode.
Using Apaches httpd.conf is faster since accessing the .htaccess file adds a small overhead—Apache checks every directory—and parent directory—for the file and it will be loaded for every request.
Using the httpd.conf is better when you have access to it. Use .htaccess if you don't have access to the main configuration file.

Resources