Symfony2 - Rewrite url to add www prefixwith existing .htacess file - apache

Symfony2 core configuration comes up with the following .htacess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
I understand that this will erase the app.php from the url in the browser.
I would like to add the following rewrite rule to add www. to my WebSite everytime a user enter the domain name without the www. :
RewriteCond %{HTTP_HOST} ^valutao\.com$ [NC]
RewriteRule ^(.*)$ http://www.valutao.com/$1 [L,R=301]
So now i have the following .htacess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
RewriteCond %{HTTP_HOST} ^valutao\.com$ [NC]
RewriteRule ^(.*)$ http://www.valutao.com/$1 [L,R=301]
</IfModule>
My issue is that app.php will show up again. It seems that my rule just hides the base configuration. Here is an example of what is displayed in the url when I enter valutao.com : http://www.valutao.com/app.php
Does someone know how to fix that. Unfortunately I have just basic knowledge with the rewrite rules.
Thanks for help

You simply need to change the order (www rewrite should go first).
I'd also make it less dependent on the domain name:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>

Related

htaccess redirect directory to single page in same directory

All attempts failed... I want http://olex-design.at/journal to to be redirected to http://olex-design.at/journal/alle through .htaccess.
So far I have the following - non working - rule in place which produces a 403 forbidden page.
RewriteRule ^journal/$ /journal/alle [R=301,L]
The .htaccess file is placed in the root directory. Maybe someone can point me towards the right direction...
All rules in .htaccess files:
Options -Indexes -MultiViews
RewriteEngine on
#Force non-www:
RewriteCond %{HTTP_HOST} ^www\.olex-design\.at [NC]
RewriteRule ^(.*)$ http://olex-design.at/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]
RewriteRule ^journal/$ /journal/alle [R=301,L]
ErrorDocument 404 /404
With the support of my webhost I was able to resolve my issues described above. This is the complete code in my .htaccess file.
Please note that the first 4 lines did the trick. The code below is responsible to remove the "www", remove the "index" when your in home (http://olex-design.at) and remove the .html file extension.
RewriteOptions inherit
RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^journal\/?$ "http\:\/\/olex\-design\.at\/journal\/alle" [R=301,L]
AddDefaultCharset utf-8
Options -Indexes -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.olex-design\.at [NC]
RewriteRule ^(.*)$ http://olex-design.at/$1 [L,R=301]
<IfModule mod_rewrite.c>
#index redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\ HTTP/
RewriteRule ^index\.html$ http://olex-design.at/ [R=301,L]
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

htaccess subdirectory + 301 on www

I'd like to do 2 things with htaccess but I can't figure out how to do it.
Let's say my domain is domain.com
First, I'd like to force www. in the url with a 301 redirect.
Another thing, my website is not hosted in the root directory but in /laravel/public/
So I'd like to set this subdirectory as root, and remove it from the URL if someone try www.domain.com/laravel/public/ => www.domain.com
How can I do that?
Thanks in advance.
Assuming that you can't change the document root to point to your public folder, you can try adding these rules to document root (not your public folder):
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{THE_REQUEST} \ /+lavarel/public/([^\?\ ]*)
RewriteRule ^ /%1 [L,R=301]
RewriteCond %{REQUEST_URI} !^/laravel/public/
RewriteRule ^(.*)$ /laravel/public/$1 [L]
You'll need to have your own .htaccess file in the root of your domain, along with another in the laravel/public directory (there already is one there, but you need to modify it.
Below are the two files in full as I have them on my testing server.
/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Rewrite requests to the laravel/public index
RewriteCond %{REQUEST_URI} !^/laravel/public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /laravel/public/$1 [L]
</IfModule>
/laravel/public/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Force www. (also strips laravel/public)
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Strip laravel/public, prevent loops
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_URI} ^/laravel/public(/.+)/? [NC]
RewriteRule ^ %1 [R=301,L]
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

htacess Proxy Redirect /a to /b, 301 redirect of /b to /a

I have website built on a bespoke centralised legacy CMS.
It has a section which works under the URL /database
For one site I'd like to change that to use /blog and it still needs to tell the CMS that it's database.
So for .htaccess I have
RewriteRule ^blog$ /database [P,L]
RewriteRule ^blog/(.*)$ /database/$1 [P,L]
Which works great. But I also need to tell google that the site has changed and not to look at the old one.
So I need /database to 301 to /blog.
So:
RewriteRule ^database$ /blog [L,R=301]
I've tested this with http://htaccess.madewithlove.be and it says it's fine, but it's causing an infinite loop in the browser.
To be honest I feel like part of the problem is that I can't really express the question very well, especially the title, so any feedback on that would be appreciated.
Here's the rest of the file just in case that's relevant
# redirect trailing slashes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.+)?fromMobile=true$ [NC]
RewriteRule (.*) /$1?%1&%2 [R=301,L]
RewriteRule ^(.+[^/])/$ http://%{HTTP_HOST}/$1 [R=301,L]
#uk.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^iflg\.uk\.com$ [NC]
RewriteRule ^(.*)$ http://www.iflg.uk.com/$1 [R=301,L]
RewriteRule ^blog$ /database [P,L]
RewriteRule ^blog/(.*)$ /database/$1 [P,L]
#RewriteCond %{REMOTE_ADDR} !=localhost
#RewriteRule ^database$ /blog [L,R=301]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~iflg/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /~iflg/index.php
</IfModule>
You need to exempt the second rule to prevent the proxy server from being redirected too.
RewriteCond %{REMOTE_ADDR} !=127.0.0.1
RewriteRule ^database$ /blog [L,R=301]
Do not rely on that htaccess tester, it is too simplistic.

.htaccess ensure www redirect incorrect

I am trying to ensure that urls always have www in front of them for canonicalization reasons.
Unfortunately when I put the following url in:
http://website.net/handbags/1/12/this-is-some-text
It redirects me here:
http://www.website.net/?controller=handbags&path=1/12/this-is-some-text
I would like to add it works fine when using:
http://www.website.net/handbags/1/12/this-is-some-text
I want it to redirect me here...
http://www.website.net/handbags/1/12/this-is-some-text
I'm not sure what could be causing this error. Here is my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# CORE REDIRECT
RewriteRule ^([a-zA-Z]*)/?(.*)?$ index.php?controller=$1&path=$2 [NC,L]
# ENSURE WWW
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+\.[a-z]{2,6})$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# ENSURE WWW
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+\.[a-z]{7})$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
# CORE REDIRECT
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]*)/?(.*)?$ index.php?controller=$1&path=$2 [NC,L]
</IfModule>
You can do what I've done above; however, it's just going to redirect to http://www.website.net/handbags/1/12/this-is-some-text and then it's immediately going to hit the "CORE REDIRECT" rule and send you to http://www.website.net/?controller=handbags&path=1/12/this-is-some-text.
Is there a reason you don't want it to redirect to "?controller=handbags" when they go to www?
UPDATE
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# ENSURE WWW
Rewritecond %{HTTP_HOST} !^www\.website\.net
RewriteRule ^(.*)$ http://www.website.net/$1 [R=301,L]
# CORE REDIRECT
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]*)/?(.*)?$ index.php?controller=$1&path=$2 [NC,L]
</IfModule>
Whenever your # CORE REDIRECT matches, the other rules are not even checked, as the core redirect rule has the Lflag set. That ones tells Apache to stop trying other rules. Remove that flag or change the rule. ;)

.htaccess - won't add www to only one subdirectory

I have a .htaccess in root directory, with this content:
<IfModule mod_rewrite.c>
RewriteEngine on
#RewriteCond %{HTTP_HOST} ^domain.com
#RewriteRule (.*) http://www.domain.com$1 [R=301,L]
</IfModule>
If I go to http://domain.com/subdir it rewrites to http://www.domain.com/subdir.
But I have a problem with one subdirectory named "crm" - if I go to http://domain.com/crm it redirects me to http://www.domain.com instead of http://www.domain.com/crm.
In this "crm" subdirectory I have another .htaccess file, which rewrites .php extension to .html. Here is the code:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([^/]*)/([^/]*)\.html$ ?name=$1&id=$2 [L]
RewriteRule ^(.*).html$ index.php?name=$1 [QSA]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]*)/([^/]*)\.html$ ?name=$1&id=$2 [L]
</IfModule>
Can someone please tell me how can I make this work? So when I will go to http://domain.com/crm it will redirect me to http://www.domain.com/crm.
EDIT:
Root .htaccess was ok, I was changing something and forgot to remove comments for stackoverflow.
crm/.htaccess is now:
RewriteEngine on
RewriteOptions Inherit
RewriteRule ^([^/]*)/([^/]*)\.html$ ?name=$1&id=$2 [L]
RewriteRule ^(.*).html$ index.php?name=$1 [QSA]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]*)/([^/]*)\.html$ ?name=$1&id=$2 [L]
But it still doesn't redirect this subdir to www.domain.com/crm but only to www.domain.com :(
First change root .htaccess to this: (seems to be commented at present)
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Then in crm/.htacess add this line just below RewriteEngine on:
RewriteOptions Inherit
UPDATE: Your crm/.htaccess is faulty. Replace that content with code from below:
RewriteEngine on
RewriteOptions Inherit
RewriteRule ^([^/]*)/([^/]*)\.html$ ?name=$1&id=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)\.html$ index.php?name=$1 [QSA,L]
Once this code is there redirect will happen as expected.