Redirect with www not working - php

I recently switched from a shared hosting to a VPS.
Everything is working fine and I can access al of my domains.
But I seem to have 1 problem:
In the past all non-www url's where redirected to url's with www.
I did this with the following htaccess.txt code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.tx3\.be$ [NC]
RewriteRule ^(.*)$ http://www.tx3.be/$1 [R=301,L]
But I'm using a new htaccess at the time with the following code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^off(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
But both are not working on redirecting my non-www domain.
When I visit my non-www domain, I get the following page:
"Apache is functioning normally"
I tried using the DirectAdmin redirect for redirecting the non-www domain to a domain with www, but that also didn't work.
Maybe I've got it all wrong and it wasn't a htaccess problem in the first place.
Did I forget something in my server settings?
I don't know, everything looks to be in order.
(I did double check my DNS settings and they appear to be fine)
Does anyone know what I should be doing next?

Please try this
Rewriteengine on
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

If using windows download putty (ssh client) give it your servers ip address select type as ssh and click open.
It'll ask you for a username and password give it your root username and password (should have been provided by vps hosting company)
Now cd through to your apache config folder (I'm only familiar with debian based setups if you're using a redhat based setup you may have to google correct path) /etc/apache2/sites-available
Now look at the directory listings (ls) and it'll show you the apache configs available chances are you'll have something like www.tx3.be in there. Edit that file I recommend using pico or nano by doing pico
Here's an example off my server
root#hinch:/home/hinch# cd /etc/apache2/sites-available
root#hinch:/etc/apache2/sites-available# ls
default
default-ssl
root#hinch:/etc/apache2/sites-available# pico default
Now you're in your apache config file you'll see something like this
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin root#yourdomain.com
DocumentRoot /var/www/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm default.htm default.html default.php
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined
</VirtualHost>
After the line that says ServerAdmin add in the following
ServerAlias tx3.be
ServerAlias www.tx3.be
Save the file and restart apache
service apache2 restart or service httpd restart
Delete all redirect code from your .htaccess and make sure you have both the www. dns A record and the #. A record pointing to your servers ip address. Now regardless of which version of the domain the user goes to they'll see your site.

Related

Symfony (3.4) app : cant redirect requests to /web dir via .htaccess

I've a website lets say website.fr ( on server : /web/website ) ;
I ve added a symfony blog app in /web/website/blog/.
I dont manage to access the blog via website.fr/blog/ (404 error)
There is a .htaccess file in /web/website/blog/ directory, and one in /web/website/blog/web directory.
I am used to deploy symfony apps 'alone' on a server, and make the domain point to the web dir of the app, and everything always works fine, but in this configuration, I cant get it to work.
I have tried to use the following .htaccess conf under /web/website/blog/ :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ web/$1 [QSA,L]
</IfModule>
And I did not edit the default one under /web/website/blog/web/
Im not sure what Im doing wrong, so any help would be appreciated !
For me I use .. nano /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
ServerName domain.tld
ServerAlias www.domain.tld
DocumentRoot /var/www/project/web
<Directory /var/www/project/web>
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
# uncomment the following lines if you install assets as symlinks
# or run into problems when compiling LESS/Sass/CoffeeScript assets
# <Directory /var/www/project>
# Options FollowSymlinks
# </Directory>
ErrorLog /var/log/apache2/project_error.log
CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>
which is explained Here and restart Apache, but I do not know if I understand your problem
I couldn't get what I wanted, but here is the solution I implemented :
I created a .htaccess in /web/website dir, that rewrites all requests to /web/website/blog, with the root (/) excluded from this rewriting :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ blog/web/$1 [QSA,L]
</IfModule>
That's not the way I wanted to get my rewriting, but I have the result I expected ; note that it would not be a good solution for a website that would evolve with new modules, routes, folders...

Subdomain redirects to the main domain (Apache2)

I asked this question to DigitalOcean questions section but did not get an answer, so I am trying my luck with you guys. Here it goes:
I know this question has been asked at least a million times, but I tried all the solutions or recommendations that I could find and it still doesn't work.
The droplet is using Ubuntu 16.04, I am trying to setup sub domain with a virtual host in Apache2.
At first, I followed this DigitalOcean tutorial How To Set Up Apache Virtual Hosts on Ubuntu 16.04.
On top of that, I created A record in domain panel with a hostname of sub.example.com pointing to my droplet.
I noticed when I type sub.example.com shows my main domain, which is on main domain or exactly running on Nginx. URL in URL bar stays sub.example.com
I checked my /etc/hosts file and added an entry like this (last one):
127.0.1.1 blog blog
127.0.0.1 localhost
123.345.554.21 www.sub.example.com sub.example.com
My virtual host file looks like this:
ServerAdmin admin#example.com
ServerName sub.example.com
ServerAlias www.sub.example.com
DocumentRoot /var/www/sub.example.com/public_html/
<Directory /var/www/sub.example.com/public_html>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
I used a2ensite command to enable the site. I tried using the command with .conf extension and without. I disabled default .conf file (000-default or something like that).
I tried adding .htaccess file in my project directory with this in it:
RewriteEngine on
# redirect to https www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)(domain\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]
# redirect to http subdomain
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^((?!www).+\.sub.domain\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
Paths to the document root are correct and double-checked, and even if I change to incorrect route, nothing happens. I restarted apache2 service and droplet more times than I can count. I added permissions to the directory.
I don't know what should I do anymore. Maybe I am missing something? Is it maybe because my index file is a .php file?
Anyways, thank you for your help!

URL Rewriting and Laravel 4 Routes not working on server?

I bet this question has been asked time and time again, but I just can't seem to get an answer. Anyway, here is my situation. I have a domain, lets say example.com. Example.com works fine, everything works as it should. However, if I navigate to Example.com/about, which works on my local machine, I get a 404 error. However, if I go to example.com/index.php/about, the about page works fine. I know this must be a mod rewrite issue. I have enabled mod rewrite and restarted my server to no avail. Anyway, here is some code:
Virtual Host:
<VirtualHost *:80>
ServerName example.com
DocumentRoot "/var/www/example/public"
<Directory "/var/www/example/public">
AllowOverride all
Allow from All
</Directory>
</VirtualHost>
And here is my .htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I'm using Ubuntu 14.04 on a Digital Ocean Droplet.
Thanks!
1)
go to /etc/apache2/sites-available and create file and add below
<VirtualHost *:80>
ServerName example.com
ServerAdmin webmaster#newyear
DocumentRoot /var/www/html/example/public
<Directory /var/www/html/example/public>
options Indexes FollowSymlinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
2) Create Link of above created file and paste it into /etc/apache2/sites-enabled
3) Check /etc/apache2/mods-available for rewrite.load file
if it is not there create it and add below line & save
LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so
create link of rewrite.load file and paste it into etc/apache2/mods-enabled
4) open /etc/hosts
add below
127.0.0.1 example.com
5) sudo service apache2 restart
I figured out how to do this, I had to put my VirtualHost code in sites-enabled, not sites-available. Facepalm.
In ubuntu 14.04 with apache2 i was fall this problem.Then i solve this and wrote a blog,you can follow this
http://www.kingpabel.com/apache-mod_rewrite/

running laravel on a different port

I'm using Bitnami MampStack on OS X 10.9. Because this is an inherited laptop, I had to set up Apache under MAMP to listen on port 8888. I tweaked it to listen on 8889 as well and added the following as a VirtualHost in my httpd.conf file:
<VirtualHost *:8889>
DocumentRoot "/Applications/mampstack-5.4.26-0/apache2/htdocs/codebright/public"
ServerName localhost
<Directory "/Applications/mampstack-5.4.26-0/apache2/htdocs/codebright/public">
Require all granted
</Directory>
</VirtualHost>
I followed the example here (so all my source code matches his). The index blade works, but the edit, create, and delete blades return a 404.
As I was testing, I discovered that http://localhost:8889/create returned a 404, but http://localhost:8889/index.php/create returned the correct view. Also, browsing http://localhost:8888/codebright/public/create works as expected.
So...me being kinda new to Laravel and MVC frameworks in general, is there some way I can have this installing running properly on port 8889?
Your virtualhost is working as it should, you just have now to be sure your public/.htaccess file is in place:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
This is the file that rewrites your urls removing the /index.php from them.
EDIT
Also, check if you have mod_rewrite installed and enabled, because your .htaccess uses it, in Ubuntu/Debian, to enable it, you have to execute:
sudo a2enmod rewrite
Figured it out, thanks to this. I was missing AllowOverride All in the <Directory> section. Updated httpd.conf:
<VirtualHost *:8889>
DocumentRoot "/Applications/mampstack-5.4.26-0/apache2/htdocs/codebright/public"
ServerName localhost
<Directory "/Applications/mampstack-5.4.26-0/apache2/htdocs/codebright/public">
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
I thought I might have been missing RewriteEngine On in the VirtualHost definition but it doesn't seem to have an effect on my problem.

URL Rewriting not working with ZF and apache2

This has got me baffled. I recently set up VM in a hurry to go travelling. I installed Ubuntu 12.04 and LAMP. The PHP version is 5.5 and Apache is 2.4. I have used this same configuration on my home machine and it works fine.
Anyhow, the problem is that I can only access actions on the index controller of my ZF application, e.g: domainname/, domainname/index/info. If I try any other controllers, e.g. domainname/test/view, I get the apache 404 error. (Note - NOTE the ZF Not Found error).
I imagine this is a problem with URL rewriting, but the .htaccess on my VM is the same as my home machine, and I can't see anything in the virtual host configuration that would cause this. Also, if it was simply an URL rewriting problem, I would have thought it would come in to play for ALL urls, including the index controller...
Here's my htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Ideas?
EDIT: Here's the virtual host config:
DocumentRoot /home/kim/www/vhost/public
ServerName koop
SetEnv APPLICATION_ENV "development"
<Directory /home/kim/www/vhost/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
Ok, so I had to change AllowOverride to All. That means Apache2 will now process my .htaccess files.
Then I needed to enable mod rewrite, by sudo a2enmod rewrite, then restart apache, i.e. sudo service apache2 restart

Categories