rewriting to always use www in domain ( even on subpages ) - .htaccess

I've added the following to my .htaccess to properly redirect any visitors from http://domain.com to http://www.domain.com ... but I still can't get it to work with subpages...ie http://domain.com/subpage is not yet redirecting to http://www.domain.com/subpage .
Here is what I have in my htaccess now:
<Files wp-config.php>
order allow,deny
deny from all
</Files>
<Files .htaccess>
order allow,deny
deny from all
</Files>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} !^www
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [L,R]
</IfModule>
So the question is what else would I need to add or update in my htaccess to get this working?
Many thanks for your suggestions!

You have the redirect happening after you've done routing to index.php, it needs to happen before any routing rules get executed. Try swapping them around:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [L,R]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
(also added the [NC] flag so case isn't checked in hostname match)

Related

htaccess rewrite rule for url with querystring

I just did a huge makeover to a site and changed the framework. Because of that, the structure of urls changed. This is not a huge problem except for one url which is used a lot. I need to redirect user from the old address to the new one, and I just cannot figure out how to do that.
Old url: domain/?page/subpage/subsubpage.html?param=[parameter]/
New url: domain/controller/function/[parameter]/
My rewrite rules currently are this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [L]
RewriteRule ^/\?page/subpage/subsubpage.html\?param=(.*)$ /controller/function/$1 [L]
but it does not work, no redirect is happening.
Edit: Added the whole htaccess file! This currently gives me error 500 ( "Request exceeded the limit of 10 internal redirects due to probable configuration error.")
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Allow asset folders through
RewriteRule ^(fuel/modules/(.+)?/assets/(.+)) - [L]
RewriteRule ^(fuel/modules/(.+)?/tuki/(.+)) - [L]
RewriteRule ^(fuel/modules/(.+)?/wiki/(.+)) - [L]
# Protect application and system files from being viewed
RewriteRule ^(fuel/install/.+|fuel/crons/.+|fuel/data_backup/.+|fuel/codeigniter/.+|fuel/modules/.+|fuel/application/.+) - [F,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/\?page/subpage/subsubpage.html\?param=(.*)$ /controller/function/$1 [L]
RewriteRule .* index.php/$0 [L]
# Prevents access to dot files (.git, .htaccess) - security.
RewriteCond %{SCRIPT_FILENAME} -d
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule "(^|/)\." - [F]
</IfModule>
Options -Indexes
With your shown samples/attempts could you please try following.
Please make sure you clear your browser cache before testing your URLs.
Options +FollowSymLinks
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine ON
RewriteBase /
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Prevents access to dot files (.git, .htaccess) - security.
RewriteRule \.(git|htaccess) - [F]
# Allow asset folders through
RewriteRule ^fuel/modules/(assets|tuki|wiki) - [L]
# Protect application and system files from being viewed
RewriteRule ^fuel/(install|crons|data_backup|codeigniter|modules|application)/.+ - [F,L]
##Any non existing directory OR files whose URI srarts from page rule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/\?page/subpage/subsubpage.html\?param=(.*)$ /controller/function/$1 [L]
####If any non existing directory OR file is requested then come here:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php/$0 [L]
###New rule added by me here.....
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
ReWriteCond %{QUERY_STRING} .*=([^/[]*)/?$
RewriteRule ^ controller/function/%1 [L]
</IfModule>

Set 410 list of url with space

I have list of url with space on it , And i want to set them as gone (410)
https://www.website.com/Enhanced Category/religions/page/28
https://www.website.com/Enhanced Category/
Im on shared hosting using litespeed server...tried to encode the space on my htaccess
https://www.website.com/Enhanced%20Category/religions/page/28
https://www.website.com/Enhanced%20Category/
but it failed, the url still serve as 404...can anyone help me?
My htaccess
RewriteEngine On
Redirect 410 /Enhanced%20Category
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
RewriteRule ^\d{4}/\d\d/([\w-]+\.html)$ /$1 [R=301,L]
<Files xmlrpc.php>
Order Allow,Deny
Deny from all
</Files>
Options All -Indexes
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Requested url
https://www.website.com/Enhanced Category return 404 status
https://www.website.com/Enhanced%20Category return 410 status
Based on your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs, fair warning haven't tested full htaccess file since there are other rules mentioned by OP with my added ones.
<Files xmlrpc.php>
Order Allow,Deny
Deny from all
</Files>
<IfModule mod_rewrite.c>
RewriteEngine ON
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
RewriteRule ^\d{4}/\d\d/([\w-]+\.html)$ /$1 [R=301,NE,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^Enhanced%20Category/religions/page/28/?$ - [R=401,L]
RewriteRule ^Enhanced Category/?$ - [R=401,L]
</IfModule>

.htaccess redirect to different alias

I want to redirect the alias Eg.
http://test.com/mp3 to https://test.com/audio
I had tried this code
<FilesMatch "\.(htaccess|zip|rar)$">
Order Allow,Deny
Deny from all
</FilesMatch>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^mp3$ /audio? [L,R=301]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L,QSA]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
Not work for me . Problem is when i search my site on google and click on any search result it will goes to https://test.com/mp3
Please Help
Thanks
Krishna Karki

htaccess pretty url to regular php file

I am trying to use my htaccess to allow me to send in a pretty url and have it route to a regular webpage.
So while I am sending in "http://www.foobar.com/dir1/file/id" I would like it to route to "http"//www.foobar.com/dir1/file.php?id=1" As I mentioned, I have been trying to achieve this through my htaccess file but I am just getting 404's all the time. I was hoping one of the gurus here can help me.
This is my htaccess on the dir1 directory:
Options +FollowSymLinks
RewriteEngine on
RewriteEngine on
RewriteRule ^dir1/file/([a-zA-Z0-9]+)$ file.php?id=$1 [L]
Thanks!
Try This its working fine
//First Parameer
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ file.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ file.php?id=$1
//Second Parameter
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ users.php?user=$1&page=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ users.php?user=$1&page=$2
Or try this
Options +FollowSymLinks
RewriteEngine On
# add trailing slash
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
# rewrite gallery/ to gallery.php
RewriteCond %{REQUEST_URI} /$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1.php [L]
# redirect example.com/index.php to example.com/
RewriteCond %{REQUEST_URI} index\.php$
RewriteRule ^(.*)index\.php$ /$1/ [R=301,L]
try this:
<FilesMatch "\.(tpl|ini|log)">
Order deny,allow
Deny from all
</FilesMatch>
here you can change (tpl). file into your needs
and further
# SEO URL Settings
RewriteEngine On
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

.htaccess redirect from specific file to an already defined rewriterule

I've been checking around but I can't seem to find the exact syntax to achieve what I'm trying.
I'm currently renewing a website, but as it has pretty good positions in Google I want to preserve all the links, but this time I want to use pretty url's. So, the problem I'm having is the following:
I have this bases-generales (general-rules) section and I use this to Rewrite:
RewriteRule ^bases-generales/ bases.php?locale=es_LA
The problem I'm having is that the current (old) file is named bases.php, so I want users to be redirected to /bases-generales/ when they open bases.php directly from Google or some other link.
The rule I'm using at the moment is:
RewriteRule /bases.php http://mysite/bases-generales/ [R=301,L]
Now, this command does redirect, but the only problem is that the url looks like this:
http://mysite/bases-generales/?locale=es_LA
And the other problem is that it causes a Redirect Loop.
For more information this is the complete htaccess (mysite is fake of course)
<Files ~ "^\.(htaccess|htpasswd)$">
deny from all
</Files>
order deny,allow
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(mysite)(:80)? [NC]
RewriteRule ^(.*) http://mysite/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mysite/$1/ [L,R=301]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteBase /
RewriteRule ^/ index.php?locale=es_LA
RewriteRule ^bases-generales/ bases.php?locale=es_LA
RewriteRule ^condiciones/ condiciones.php?locale=es_LA
RewriteRule ^programa/ programa.php?locale=es_LA
RewriteRule ^premios/ premios.php?locale=es_LA
RewriteRule ^ganadores/ ganadores.php?locale=es_LA
RewriteRule ^patrocinadores/ patrocinadores.php?locale=es_LA
RewriteRule ^galeria/ galeria.php?locale=es_LA
RewriteRule ^contacto/ contacto.php?locale=es_LA
RewriteRule ^bases.php http://mysite/bases-generales/ [R=301,L]
concerning your first request, you have to append ? to the new url in order to prevent Rewrite_mod from adding the querystring, so the directive will be
RewriteRule ^bases.php http://mysite/bases-generales/? [R=301,L]
2) you should add L to your rewrite rules to stop processing the url :
RewriteRule ^bases-generales/ bases.php?locale=es_LA [L]
the htaccess will be:
<Files ~ "^\.(htaccess|htpasswd)$">
deny from all
</Files>
order deny,allow
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mysite/$1/ [L,R=301]
RewriteBase /
RewriteRule ^bases.php http://mysite/bases-generales/? [R=301,L]
RewriteRule ^sample2.php http://mysite/sample2/? [R=301,L]
RewriteRule ^bases-generales/ bases.php?locale=es_LA [L]
RewriteRule ^sample2/ sample2.php?locale=xx [L]

Resources