.htaccess Messy Rewrite Rules - apache

I could do with some assistance trying to clean up an .htaccess file that has been running on one of our servers at work.
It is currently set up so that if someone types example.com it will redirect to www.example.com. The problem is that we want to utilise subdomains but when we try to add a subdomain, like beta.example.com it will redirect to www.beta.example.com
Here is the .htaccess file
DirectoryIndex index.html index.php
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
I have tried multiple variations from questions here on SO and via Google and htaccess generators and none of them seem to help as they usually put the site in a redirect loop.
Any help on getting the configuration correct would be appreciated.
-- Chris

To automatically add a www to your domain name when there isn't a subdomain, add this to the htaccess file in your document root::RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]

Related

.htaccess redirect issue with multiple .htaccess file

I am using .htaccess file like below for redirect www to non www on my public_html directory
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
its working fine and redirect like below
www.example.com
to
https://example.com
Now in my sub directory called latest, I have another .htaccess file like below
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
which is making my url clean like below
www.example.com/latest/index.php?page=1
to
www.example.com/latest/1
but even I have .htaccess file in home directory which is making www to non www, my this directory does not redirect www to non www.
I want redirect
www.example.com/latest/1
to
https://example.com/latest/1
I am sure .htaccess file in my directory is causing issue but I do not know how to resolve it. Let me know if anyone here can help me for the same.
Thanks!
The problem is, mod_rewrite is not inherited (at least by default). If you have a rewrite rule in the "default" directory .. You're going to have to include that rule in all subsequent directories "below" that. I have never seen (or heard of) a setting for allowing mod_rewrite to allow inheritance from parent .htaccess files. In general, this is just accepted as "the way it is".
UPDATE
THIS QUESTION In Server Fault explains this as well. The mod_rewrite rules from the previous htaccess files don't even get processed.
COMBINE THE 2 in the SUB DIRECTORIES
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# add this only to sub directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]

htaccess rewrite after redirect

I have a site which uses htaccess to rewrite all pages to the index page with a hash which is then used to serve up content. The file looks like this....
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php?urlpath=$1 [NC,L,QSA]
I am now moving some of the pages of the site, however if I add a redirect such as....
Redirect 301 /blog /new_location/blog/
I am running into problems with the resulting url looking like
https://mydomain/new_location/blog/urlpath=blog.php
Can anyone suggest a way that I get the page to redirect to mydomain/new_location/blog/ and then run the rewrite on the new url.
Many thanks
RewriteRule and Redirect are from different Apache modules, so run at different times in the processing, not in the order they appear in the configuration. You're best off sticking to one module or the other, by using the [R] flag to RewriteRule.
RewriteRule /blog(.*) /new_location/blog$1 [R=301]
OK, I managed to get this working using a combination of Redirect and Rewrite like so....
Redirect 301 /blog /new_location/blog
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/blog
RewriteRule ^([^?]*)$ /index.php?urlpath=$1 [NC,L,QSA]
Maybe not the neatest solution, but it work!

Redirect secured domain to secured subdomain using htaccess

I have 3 domains and one hosting. I am trying to use the same hosting for all of my three domains through htaccess. So, I created a subdomains with that name on my domain linked with hosting, which looks something like below.
www.site1.com [Main domain linked with hosting]
site2.site1.com [subdomain for www.site2.com]
site3.site1.com [subdomain for www.site3.com]
What I want to achieve is, user shouldn't go to subdomain site2.site1.com, instead they would be able to go to www.site2.com only and request will be sent to site2.site1.com at backend.
Up to here, all is done and worked well. The only problem comes afterwards, when I adds SSL on site. I have SSL for all of these domain and subdomain. If a user visit non-ssl, then he should be redirected to SSL one. Some of SSL works well but when I add SSL for all of them, then I start getting 500 error.
Here is my .htaccess file
RewriteEngine On
DirectoryIndex index.php
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^site2.com
RewriteRule ^(.*) https://site2.site1.com/$1 [P]
RewriteCond %{HTTP_HOST} ^www.site2.com
RewriteRule ^(.*) https://site2.site1.com/$1 [P]
RewriteCond %{HTTP_HOST} ^site3.com
RewriteRule ^(.*) https://site3.site1.com/$1 [P]
RewriteCond %{HTTP_HOST} ^www.site3.com
RewriteRule ^(.*) https://site3.site1.com/$1 [P]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
As I add https on redirection link, so it start giving me 500 error whereas when I make redirection to http then it will not load my page because non-secured site http://site2.site1.com will be loaded over secured https://www.site2.com and in a result, nothing will be shown.
Here I need help to resolve this problem. I have looked over different questions but haven't found any question relevant to me because I need to keep my .htaccess working with redirection and SSL. Moreover, I also need to redirect to www one, if not added in URL.
Any help will be appreciated.
Like, all the times I experienced here I have resolved my problem myself. The resolution to my problem was the following htaccess
RewriteEngine On
DirectoryIndex index.php
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^site2.com
RewriteRule ^(.*) site2/$1
RewriteCond %{HTTP_HOST} ^www.site2.com
RewriteRule ^(.*) site2/$1
RewriteCond %{HTTP_HOST} ^site3.com
RewriteRule ^(.*) site3/$1
RewriteCond %{HTTP_HOST} ^www.site3.com
RewriteRule ^(.*) site3/$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
In this way instead of pointing the URL to subdomain having https I have reference that to the directory instead because path will be same for that, and now when I enter the URL with https page opens and don't give any error.

Cleaning urls my htaccess

I have looked for answers but none have worked for me. I want to clean up my urls removing the page name, extension and variables on all my pages in all folders.
so
http://www.example.co.uk/home/index.php?username=ben&id=1
to
http://www.example.co.uk/home/
and
http://www.example.co.uk/profile/index.php?blog=test&id=45
to
http://www.example.co.uk/profile/
ect....
im running apache
thanks
sorry for late reply. so far i have
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z.]+\.)?example\.co\.uk$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.example.co.uk%{REQUEST_URI} [R=301,L]
To force the www. and also remove the file name and extension
You need to enable mod rewrite module in your apache configuration.
In your .htaccess file try some basic rewrite rule.
If you rewrite
http://www.example.co.uk/home/index.php?username=ben&id=1
to
http://www.example.co.uk/home/
you are losing the parameters "username=ben&id=1".
Some rewrite tutorials from google sesarch
https://www.google.com/search?q=mod+rewrite+tutorial&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a&channel=fflb
http://www.sitepoint.com/guide-url-rewriting/
http://net.tutsplus.com/tutorials/other/a-deeper-look-at-mod_rewrite-for-apache/

removing www and index.php from url in symfony

I've been searching for like 3 or 4 hours without any result(before searching I played with rules for an hour but couldn't do it)
I don't know if you've noticed or no but google uses www like this
when it has no subdomain it will be www.google.com/blabla and
when there is a subdomain it will be earth.google.com/blabla
This is the first part
And the second part, as you know in symfony urls are like domain.com/index.php/test and thanks to the symfony .htaccess file you can access it via domain.com/test
So here is what I tried so hard to achieve
domain.com/test redirect to www.domain.com/test
www.sub.domain.com/blabla redirect to sub.domain.com/blabla
www.sub.domain.com/ redirect to sub.domain.com (without any index.php XD)
One of the annoying problems I had was redirecting from domain.com/ to www.domain.com was that after redirect it was like www.domain.com/index.php (And I hate index.php :P)
So is there any way with one redirect solve this problem?
I'm sure I'm not the only one who needs something like this and might be an idea for other people who are going to have their site with symfony or other frameworks
Thanks
Here is my complete htaccess file
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
# The admin subdomain returns to the backend
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^admin\.mydomain\..*
RewriteRule ^(.*)$ backend.php [QSA,L]
# uncomment the following line, if you are having trouble
# getting no_script_name to work
#RewriteBase /
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301]
# we skip all files with .something
RewriteCond %{REQUEST_URI} \..+$
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule .* - [L]
# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
In your VHOST config:
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^/(.*) http://domain.com/$1 [R=301,L]
Also note that from a esthetical point of view you might prefer to remove the www., looking from the technical angle (DNS, cookies, ...), it is always better to prefix with www., and redirect in the opposite way.