removing www and index.php from url in symfony - apache

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.

Related

Need to remove a specific folder from .htaccess mod-rewrite redirect rules

I'm using some software that sits in the /var/www/html folder and manages URL redirects through .htaccess (the software isn't Wordpress, but it manages URL redirects in a similar way).
I need to carve two folders (/var/www/html/folder1 and /var/www/html/folder2) out from the redirect rules, which are at the very bottom of the .htaccess file. I read through a ton of documentation but can't seem to make the following work in .htaccess (which sits in /var/www/html):
Options -Indexes
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
#force HTTPS
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#carve out these folders
RewriteRule ^/folder1($|/) - [L]
RewriteRule ^/folder2($|/) - [L]
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
</IfModule>
When I go to the directories in question (eg. https://example.com/folder1), I get: Forbidden You don't have permission to access /folder1/ on this server.
Any ideas?
I can't comment as I'm new but do the two folders have any sort of index page? Otherwise you will get a 403 response because you have disabled indexes at the top of your file excerpt.

.htaccess Messy Rewrite Rules

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]

.htaccess URL rewrite not working, tried all possible options

I was trying to rewrite a URL for making my site SEO friendly, but .htaccess rewrite not seems to work.
My URL is
www.tasteofkochi.com/dine-detail.php?a=150
I need this to look like
www.tasteofkochi.com/sometext/150
I did the simple formula but it's not reflecting, intact nothing happens at all. If I add some junk char in htaccess, site crashes, which means htaccess is working fine. Also I added a formula to remove .php extn, and that too works fine. Only issue is with this one. Can anyone please help me. I enable rewrite in httpd and allow all in directories, still not working.
Below is my .htacces
RewriteEngine on
Options +FollowSymLinks -MultiViews
RewriteBase /
## hide .php extension
# To externally redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
RewriteRule ^detail/([0-9]+)/?$ dine-detail.php?a=$1 [NC,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php?p=$1 [L,QSA]
We can create pretty urls with .htaccess and php by mainly two files one is .htaccess and another index.php
Example

Drupal redirect from installation subdirectory without creating a loop

I am working on a Drupal site that has hard coded links in lots of the content (I didn't create the site). Unfortunately that means the site has to be in a subdirectory such as "www.example.com/drupal" or the links will break. I have sucessfully redirected the site root to the subdirectory and hid it using the following code in the site root htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule (.*) drupal/$1 [L]
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
However if I go to www.example.com/drupal the subdirectory still shows up.This means that www.example.com and www.example.com/drupal have the same content which is bad for SEO. Is there some way to make /drupal redirect to root without causing a loop? I have tried just about every posted in other threads and none of them work.
You need to do the redirect before rewriting the URL. You also need to check that you're actually rewriting to a legitimate resource.
swap the rules around, the redirect should happen first
Add a check to the drupal rule
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_URI} !^/drupal
RewriteRule (.*) drupal/$1 [L]
</IfModule>

How do i force www subdomain on both https and http

For whatever reason I can't seem to get this right, I've looked at many examples on here and apache's website. I'm trying to force www.domain.com instead of domain.com on EITHER http or https but I am not trying to force https over http.
the following code seems to work for all https connections but http will not redirect to www.
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^ http://www.domain.com%{REQUEST_URI} [R=301]
You don't need the second RewriteEngine directive. That may or may not be causing a parse issue making the second set of rules not work. To test whether this is the case, try switching the order of the two blocks you have.
It's good practice to use L to modify requests that are definitely the last. So, change [R=301] to [R=301,L] both times it appears.
Largely as a matter of style, I would consider changing the RewriteRule directives to something like (using http or https as appropriate):
RewriteRule ^(.*)$ http://www.domain.com$1 [R=301,L,QSA]
Your rules seem to be fine. You can combine them as follows:
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.example.com%{REQUEST_URI} [L,R=301]
Also note the additional L flag to stop the rewriting process after this rule has been applied.
In case anyone still need an answer to this. Use another .htaccess. Get guide from here, I found it and it looks good: http://www.farinspace.com/codeigniter-htaccess-file/
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
### Canonicalize codeigniter URLs
# If your default controller is something other than
# "welcome" you should probably change this
RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
# Enforce www
# If you have subdomains, you can add them to
# the list using the "|" (OR) regex operator
RewriteCond %{HTTP_HOST} !^(www|subdomain) [NC]
RewriteRule ^(.*)$ http://www.domain.tld/$1 [L,R=301]
# Enforce NO www
#RewriteCond %{HTTP_HOST} ^www [NC]
#RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301]
###
# Removes access to the system folder by users.
# Additionally this will allow you to create a System.php controller,
# previously this would not have been possible.
# 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
</IfModule>
Remember, once you have your CodeIgniter htaccess file setup, you will want to go into your “/system/application/config/config.php”, find the following:
$config['index_page'] = "index.php";
and change it to:
$config['index_page'] = "";