Temporary URL redirect loop - .htaccess

I just bought a shared hosting account from bluehost.com & they gave me a temporary URL like http://ipaddress/~username/. The problem is that I get redirect loops when I try to access my URLs. Everything is working fine on my proper URL.
Proper URL = Accessing from the domain.
If you have any solution to this problem, please specify it will be a great help.
I have the following rewrite rules in my .htaccess files.
redirect www to non-www version
RewriteCond %{HTTP_HOST} www\.mydomain\.com$ [NC]
RewriteRule ^(.*)/?$ http://mydomain.com/$1 [L,R=301]
rewrite the domain.com/index.php to / (base-directory)
RewriteCond %{THE_REQUEST} /index\.php
RewriteRule ^(.*?)index\.php$ /$1 [R=301,NE,L]
rewrite all requests to index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,NE,L]

Related

Removing part of URL but don't redirect with htaccess

So the problem I'm looking at and haven't managed to solve!
For example the url is http://someurl.com/brands/brand
What I want to accomplish is that htaccess lets te request go to url: http://someurl.com/brands/brand but removes the "brands" part from the url.
Also if the url: http://someurl.com/brands is called the page "brands" needs to be displayed. So only when there is a brand after /brands/ it needs to do a URL rewrite to http://someurl.com/brand
I have tried this but this piece does a redirect to the new location witch doesn't exist.
RewriteRule ^onze-merken/(.*)$ /$1 [L,R=301]
So I need the above with out the redirect, it only needs to rewrite the URL.
Any help would be greatly appreciated, Thanks!
This is now my htaccess part where the rewriting is done!
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /onze-merken/(\S+)\s [NC]
RewriteRule ^ /%1 [R=302,L,NE]
# Only rewrite if the directory doesn't exist.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# internal forward from pretty URL to actual one
RewriteRule ^((?!onze-merken/).+)$ /onze-merken/$1 [L,NC]
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
RewriteRule ^(uploads/([a-z0-9_\-\.]+)/__images/custom/(.*)/)(.*)$ http://someurl.com/uploads/$2/__images/custom/$3/$4 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /brands/(\S+)\s [NC]
RewriteRule ^ /%1 [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!brands/).+)$ brands/$1 [L,NC]

Drupal: Correcting URL link access for subdirectory site Installation

I've installed Drupal 7 to a sub-directory on my server and used .htaccess mod_rewrites to fix most of the URL pathing issues, I'm now looking for a way to strip the sub-directory from the URL when it's explicitly requested.
To be clear, the site can be accessed from 'example.com' and the internal links work fine, but I'm looking to prevent requests to 'example.com/sub/file' from working, Ideally with a 301 back to 'example.com/file'.
My .htaccess at the moment looks like this:
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^$ sub/index.php [L]
RewriteCond %{DOCUMENT_ROOT}/sub%{REQUEST_URI} -f
RewriteRule .* sub/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* sub/index.php?Q$0 [QSA]
Have you tried adding:
Redirect 301 /\/sub/file http://www.example.com/file
to your .htaccess?

.htaccess redirect old URLs to new URLs, http to https, non-www to www?

I have a somewhat tall order for redirecting URLs for a new version of an existing site. Currently, I'm forcing www and redirecting all links with the following structure:
http://www.example.com/username
using the following .htaccess rules:
#Force www in all links
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTPS_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
#This rule allows for vanity URLs
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^agents/(.*) /members.php?$1 [l]
RewriteRule ^(.*) /members.php?$1 [L,QSA]
The new version of this site will be using https site-wide, and the new URL structure that should redirect any old links will look like this:
https://www.example.com/first-directory/second-directory/member
NOTE: That third rule would disappear on the new site since pretty URLs are being handled under the hood by the framework I'm using.
Taking site-wide SSL into consideration, what's the best way to achieve permanent redirects to the new URLs?
Try these rules:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /first-directory/second-directory/$1 [L,R=302]

Redirecting all requests to index.php without direct access

I have the following code in my .htaccess file.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
This redirects all requests to index.php. However I am still able to access www.mydomain.com/index.php directly from the URL. As www.mydomain.com servers the same content as www.mydomain.com/index.php will this be recorded as duplicated in google, if so how do I prevent it.
Insert this rule before existing rule to remove index.php from URI:
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=301,NC,NE]

.htaccess not redirecting from old url to new url

i have developed a website using codeigniter. Previously the site had a long URL structure so i have made them shorter in the new website.
Although i used the redirect directive in htaccess it gives me a 404 error. I have removed all the old controllers and functions.
below is a few lines from htaccess (there are many urls redirecting to new ones)
RewriteEngine On
# Redirect non-www urls to www
RewriteCond %{HTTP_HOST} ^mysite\.com [NC]
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
#RewriteRule ^([^_]*)_(.*)$ /$1-$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
Redirect 301 /payments/charity_and_donations/paid http://www.mysite.com/charity
Redirect 301 /outwards/office_furniture/damaged http://www.mysite.com/office_assets
Redirect 301 /funding/business_and_person/inward http://www.mysite.com/funds
can someone tell me why it is not redirecting from the old to the new and what am i doing wrong?
You need to have your redirecting happen before you route URIs to /index.php. Also, since Redirect is part of mod_alias and your other redirect/routing rule is mod_rewrite, the URI is being processed twice when it's not supposed to. You should just use mod_rewrite and add the rules to the beginning:
RewriteEngine On
# Redirect non-www urls to www
RewriteCond %{HTTP_HOST} ^mysite\.com [NC]
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
RewriteRule ^payments/charity_and_donations/paid http://www.mysite.com/charity [R=301,L]
RewriteRule ^outwards/office_furniture/damaged http://www.mysite.com/office_assets [R=301,L]
RewriteRule ^funding/business_and_person/inward http://www.mysite.com/funds [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
Try the below code, this works perfectly for me
RewriteCond %{HTTP_HOST} !^www\.yoursite\.com$ [NC]
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [L,R=301]

Resources