URL Canonicalization - Apache mod_rewrite - apache

I am probably attempting to take URL Canonicalization a bit too far but here it goes anyway.
Basically I am looking to the following:
301 redirect every base url to http://www.mydomain.com/ some pages are https it should recognize that and continue to use https where already used/requested
301 redirect away any trailing slashes ie http://www.mydomain.com/page/ becomes http://www.mydomain.com/page (I already have a line of code that finds the index.php page - this site is built on Codeigniter)
I Don't want the base url to have the slash stripped that is the only time a slash should be left behind
Find any instances of index.php (in the front middle or end of the url) and 301 redirect them out
ie http://www.mydomain.com/index.php/page/index.php/ becomes http://www.mydomain.com/page
301 redirect any use of my ip address to the actual domain
ie 11.11.111.111/page/index.php would become http://www.mydomain.com/page
Here is what I have so far in my htaccess file in my root directory:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{REQUEST_URI} ^(.*)(/index\.php)$
RewriteRule ^(.*)index\.php/$ http://www.mydomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(mydomain\.com)(:80)? [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^11\.11\.111\.111$
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
#This makes sure that the server actually finds the index file although its not in the url
RewriteCond $1 !^(index\.php|images|assets|downloads|css|js|robots\.txt)
RewriteRule ^(.*)$ /index\.php/$1 [L]
I am stuck right now any help would be greatly appreciated!!
Revisions!!
I have made some progress and here is what I have so far
<IfModule mod_rewrite.c>
RewriteEngine on
# index.php to /
RewriteCond %{THE_REQUEST} ^GET\ /.*/index\.(php|html)\ HTTP
RewriteRule (.*)index\.(php|html)$ /$1 [r=301,L]
# index.php to / at the base url
RewriteCond %{THE_REQUEST} ^GET\ /index\.(php|html)\ HTTP
RewriteRule (.*)index\.(php|html)$ /$1 [r=301,L]
# force www.
rewritecond %{HTTP_HOST} ^paolienvelope.com [nc]
rewriterule ^(.*)$ http://www.paolienvelope.com/$1 [r=301,L]
# force no IP
RewriteCond %{HTTP_HOST} ^70.40.204.154
RewriteRule ^(.*) http://www.paolienvelope.com/$1 [r=301,L]
#codeigniter direct
RewriteCond $1 !^(index\.php|images|assets|downloads|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
This successfully forces ip to url
Removes index.php or index.html from the url but correctly directs to the index file despite
makes sure the base url has www.
Still dont have the code to remove the trailing slash from only the request
any help would be appreciated!! Thanks!

RewriteEngine on
# index.php remove any index.php parts
RewriteCond %{THE_REQUEST} /index\.(php|html)
RewriteRule (.*)index\.(php|html)(.*)$ $1$3 [R=301,L]
# force www. (also does the IP thing)
RewriteCond %{HTTP_HOST} !^www\.paolienvelope\.com [NC]
RewriteRule ^(.*)$ http://www.paolienvelope.com/$1 [R=301,L]
# remove tailing slash
DirectorySlash off
RewriteCond $1 !^(index\.php|images|assets|downloads|css|js)
RewriteRule ^(.*)/$ $1 [R=301,L]
# codeigniter direct
RewriteCond $1 !^(index\.php|images|assets|downloads|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Let's try to solve this one point at the time. As said I'm no pro at this yet but any bit helps i guess:
I'll start with 2. because that one seems easier:
#get rid of trailing slashes
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.\.com$ [NC]
RewriteRule ^(.+)/$ http://www.mydomain.com/$1 [R=301,L]
Does this work, instead of what you have?
source:
http://blog.valtersboze.com/2009/06/add-or-remove-trailing-slash-in-url-with-htaccess/

Related

Need to redirect 301 a URL in .htaccess file but it adds extra http//?

I am trying to redirect /abc.html to /abc.php but when I did it gives an extra http// and page is not working like http//www.example.de/abc.php don't know from where this HTTP comes.
note: website is not with ssl so domain name is http://example.de
My .htaccess file
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ http://example.de/$1 [R=301,L]
RedirectPermanent /tour.html /tour.php
With your shown samples/attempts, could you please try following. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]
##To serve home page link.
RewriteRule ^/?$ index.php [L]
RewriteCond %{REQUEST_URI} !^/?$
RewriteRule ^([^.]*)\.html/?$ $1.php [NC,L]

redirect any subdomain to main domain with .htaccess

I'm attempting to redirect any direct attempts to access sub.example.com/login over to the original domain/uri at example.com/login, and from the number of questions I've already read in regards to this exact thing, it would appear easy on the surface of it...
I don't know if there's something going on that's overriding the desired outcome, but I have the following rules in my .htaccess file, yet I'm still able to access sub.example.com/login without being redirected.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com/login$ [NC]
RewriteRule ^(.*)$ http://example.com/login$1 [L,R=301]
Actually, just for complete clarity, here's my entire .htaccess file contents. Maybe somebody else can see something wrong that I'm missing.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
# redirect subdomain login attempts to main domain
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com/login$ [NC]
RewriteRule ^(.*)$ http://example.com/login$1 [L,R=301]
Put Redirect subdomain login rewrite condition & rule just after RewriteBase line. That should help to address your issue.
RewriteEngine On
RewriteBase /
# Redirect all subdomains to example.com
RewriteCond %{HTTP_HOST} !^(.*)\.example\.com$ [NC]
# Redirect only bar & foo subdomains to example.com
#RewriteCond %{HTTP_HOST} !^(bar|foo)\.example\.com$ [NC]
RewriteRule ^ http://example.com/ [L,R]

Cannot make htaccess redirect www to non-www

The 3 comments in the code explain fairly accurate what I want to achieve.
<IfModule mod_rewrite.c>
RewriteEngine On
# Change secretdiary.org/index.php?url=URL to secretdiary.org/URL on the browser's url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
# Redirect http://www.secretdiary.org/ to http://secretdiary.org/
RewriteCond %{HTTP_HOST} !^secretdiary.org$ [NC]
RewriteRule ^(.*)$ http://secretdiary.org/$1 [L,R=301]
# Add trailing slash / if there's none
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
</IfModule>
However, I am finding some problems and I think they come from putting the conditions together. When I enter www.secretdiary.org/about , it gets (showing it in the browser) to secretdiary.org/index.php?url=about, deleting the www but ignoring the first rule. Switching the order did not help at all nor messing with RewriteBase. However, if I enter normally without the www, the uri is shown normally, secretdiary.org/about, without any rewriting. Why is this and how can I fix it?
Besides, I've followed this answer and this other attempting to add automatically a trailing slash to the uri if missing. I could achieved it with PHP ( if (substr($_GET['url'], -1) != "/") header("Location: " . htmlspecialchars($_GET['url']) . '/');, but now it bothers me that I cannot achieve it with .htaccess, so if you could also spot where's the problem here it'd be very helpful.
Try this .htaccess code :
RewriteEngine On
# Change secretdiary.org/index.php?url=URL to secretdiary.org/URL on the browser's url
RewriteCond %{HTTP_HOST} ^secretdiary.org$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L]
# Redirect http://www.secretdiary.org/ to http://secretdiary.org/
RewriteCond %{HTTP_HOST} !^secretdiary.org$ [NC]
RewriteRule ^(.*)$ http://secretdiary.org/$1 [R=301]
# Add trailing slash / if there's none
RewriteRule ^([^/]*)[^/]$ $1/ [R=301,L]
I'm not sure for the last rule.
The main problem I faced was with Firefox storing the 301 redirect, which made the changes in .htaccess "not work". I deleted the cache and now it's working perfectly, although I made the trailing slash to be added with PHP to avoid headaches.
.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
# For some shady reason, this redirect should be first.
# Redirect http://www.secretdiary.org/ to http://secretdiary.org/
RewriteCond %{HTTP_HOST} !^secretdiary.org$ [NC]
RewriteRule ^(.*)$ http://secretdiary.org/$1 [L,R=301]
# Change secretdiary.org/index.php?url=URL to secretdiary.org/URL on the browser's url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
index.php:
<?php
// Redirect if there's no trailing slash
if (!empty($_GET['url']) && substr($_GET['url'], -1) != "/")
{
header ('HTTP/1.1 301 Moved Permanently');
header ("Location: http://secretdiary.org/" . htmlspecialchars($_GET['url']) . "/");
}
// The rest of the php

Apache redirect subdomain to folder, keep parameters

I have this code in .htaccess :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|gif|ico|jpg|js|png|swf|txt)$
# If empty subdomain, replace with "www"
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*) http://www.example.com/$1 [QSA,L,R=301]
# If subdomain isn't empty and not "www", redirect to "folder"
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$
RewriteRule (.*) http://www.example.com/%1/$1 [QSA,R=301]
#PAGES REDIRECTION
RewriteRule ^(.*)/register/ /index.php?sub=$1&page=register
RewriteRule ^(.*)/register /index.php?sub=$1&page=register
RewriteRule ^(.*)/lostpass/ /index.php?sub=$1&page=lostpass
RewriteRule ^(.*)/lostpass /index.php?sub=$1&page=lostpass
...
(a rule for wildcard subdmains is already in place and working)
If I browse to http://test.example.com it redirects correctly to http://www.example.com/test but when I try to browse to http://test.example.com/register, it actually redirect to http://www.example.com/test/index.php?sub=http://www.example.com/test&page=register which should redirect to http://www.example.com/test/register
What am I doing wrong here? Thanks in advance!
Try adding the L flag to your second redirect rule, similar to how have it in the first.
RewriteRule (.*) http://www.example.com/%1/$1 [QSA,R=301,L]
It looks like your rewritten URI is passing through to the next rule.
Also, I don't think your first two RewriteCond are in the correct spot.

htaccess problems with redirect for no www

I've been trying everything to manage a redirect from www.domain.com to domain.com,
but nothing seems to work for me. I always get a redirect loop - and I've tried various things I found here or on Google.
So here is my .htaccess, maybe someone could help me figure out what I can do to redirect correctly or if there is something wrong in here.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
# Redirect all to .php
# Example: example.com/hello -> example.com/hello.php
RewriteRule ^(.*)$ $1.php [L,R=301]
# show example.com/index.php always as example.com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://example.com/ [R=301,L]
Thank you so much!
I've already spent so much time trying to figure this out.
You have a rule that always matches, which is responsible for the infinite redirection. I've updated your ruleset below to fix that problem and perform the redirection you mentioned at the top of the answer. Let me know if this does what you expect.
RewriteEngine On
# Redirect www.example.com to example.com
RewriteCond %{HTTP_HOST} ^www [NC]
RewriteRule ^.*$ http://example.com/$0 [R=301,L]
# This performs an external redirection? Is that what you want?
# Don't do the rewrite if we're already pointing at a file, otherwise we'll
# just redirect over and over because .* matches what we redirect to, too
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^.+$ $0.php [L,R=301]
# show example.com/index.php always as example.com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://example.com/ [R=301,L]
The answer is Apache documentation, the documentation tell how to force usage of www. You just have to reverse the example.
RewriteCond %{HTTP_HOST} !^example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://example.com/$1 [L,R]