www. redirect is failing with .htaccess - wordpress

I have a wordpress site running on Ubuntu with apache2 server.
I want the server to redirect all traffic from www.mysite.com to mysite.com.
I made the modification in .htaccess file to look like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#RewriteEngine On
#RewriteBase /
#RewriteRule ^index\.php$ - [L]
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule . /index.php [L]
</IfModule>
And I modified apache2.conf to this:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Still no success.I still get the Ubuntu apache server default page for www.mysite.com. What am I missing?

Related

Subdomain route to subfolder with htaccess on wordpress

I am trying to create a subdomain sub.domain.com that displays domain.com/sub without changing the address bar.
I've come up with the following .htaccess rule based on other questions made here:
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^((?!sub/).*)$ /sub/$1 [L,NC]
The problem is I'm using a WordPress instance and my .htaccess currently looks like this now:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^((?!sub/).*)$ /sub/$1 [L,NC]
But when I browse sub.domain.com I'm being redirected to my homepage (domain.com) from WordPress. How can I do this correctly?
Tried to change it to:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^((?!sub/).*)$ /sub/$1 [NC]
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
</IfModule>
But it is still not working
You have to create a virtual host in apache config.
<VirtualHost *:80>
ServerName sub.domain.com
DocumentRoot /var/www/sub
</VirtualHost>
Or VirtualDocumentRoot if mod_vhosts_alias is enabled.
<VirtualHost *:80>
ServerName sub.domain.com
ServerAlias *.domain.com
VirtualDocumentRoot /var/www/%1
</VirtualHost>
In above I am pointing it to /sub directory if it exists. After all changes restart the server.
You can add below line of code in your wp-config.php files
define('WP_HOME','http://sub.domain.com');
define('WP_SITEURL','http://sub.domain.com');

Apache subfolder route not working with zf2

I have one zf2 application and one wordpress blog, both are different directories. (/var/www/zf2 , /var/www/blog). I want that my route example.com should point to my zf2 application and example.com/blog should point to my wordpress blog. I am using apache alias feature and it is working only for main route. example.com/blog is pointing to my wordpress blog but any other route of blog is pointing to zf2 application. Like example.com/blog/aboutus is pointing to my zf2 application and showing zf2 not found error page instead it should point to my blog page.
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/zf2/public
Alias /blog/ /var/www/blog/
<Directory /var/www/blog>
DirectoryIndex index.php
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
#Allow from all
</Directory>
<Directory /var/www/zf2/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
Please find below , htaccess content of my zf2 application.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
And these are my blog htaccess configuration.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Links on virtualhost with Xampp not working

I'm trying to set up a virtualhost on my machine for a wordpress project
I added this to the http-vhosts.conf in Apache
<VirtualHost *:80>
DocumentRoot "C:\xampp\htdocs\wordpresstest"
ServerName myapplication.dev
<Directory "C:\xampp\htdocs\wordpresstest">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
I added this line to the windows hosts file
127.0.0.1 myapplication.dev
And I added these lines in wp-config
define('WP_HOME','http://myapplication.dev');
define('WP_SITEURL','http://myapplication.dev');
I also added a plugin to allow relative URLs across wordpress
However, when I access http://myapplication.dev, the first page of my website is there, but none of the links work ! They all send a 500 error page
Ok, found the solution thanks to this link : https://gist.github.com/JustThomas/141ebe0764d43188d4f2
I changed my .htaccess file
Before :
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpressTest/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpressTest/index.php [L]
</IfModule>
# END WordPress
After :
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

htaccess WWW to no-WWW not working

Ok I know this hasn't been answered a lot, but I've been using a lot of various different solutions but none of them are working for me.
I'm trying to use mod_rewrite in my htaccess to redirect all www.domain.com to domain.com - standard enough.
The site is a wordpress site, and it's always worked for me without any problems using any of the following
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Or
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com [nocase]
RewriteRule ^(.*) http://domain.com/$1 [last,redirect=301]
I've tried a few other ones as well but none seem to work!
It may also be worth bearing in mind the following are also in the htaccess from wordpress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
I have also checked to see if mod_rewrite is even enabled and it appears to be based on the methods in tried here How to check if mod_rewrite is enabled in php? (Christian Roy's solution)
The server is a small droplet with Ubuntu 12.10 hosted with digital ocean if that helps? I followed this guide to ensure .htaccess was activated https://www.digitalocean.com/community/articles/how-to-use-the-htaccess-file (main change was changing AllowOveride to ALL)
Anyone any ideas? I'm lost at this stage!
Thanks
Updated htaccess file contents as requested
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
**Updated with Virtual Host File **
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /home/wordpress/public_html
Servername domain.com
ServerAlias www.domain.com
<Directory /home/wordpress/public_html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
It will work but you need to make sure 2 things:
You place www removal rule as the first rule before regular WP rules.
Update WP permalink settings to have blog/home address as without www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
should do it. As advised before, it should probably be done before WP-specific rewrites.

Redirect of www to non-www works works for all URLs except the root

I have the following code in my Apache htaccess file that is used to redirect all www URLs to non-www URLs on wordpress. This works for all URLs except the root.
For example, http://www.noshon.it/recipes redirects properly to http://noshon.it/recipes. However, http://www.noshon.it incorrectly redirects to http://noshon.it/wordpress (where it receives a 404 page).
I can't figure out why all ULRs work except for the root. Any help would be much appreciated.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.noshon\.it$ [NC]
RewriteRule ^(.*)$ http://noshon.it/$1 [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Thanks to #WebChemist for suggesting that the redirect was happening in another file. I located a file named wordpress.conf in the /opt/bitnami/apps/wordpress/conf/ directory of the Bitnami EC2 instance. This file had the following code:
# App url moved to root
DocumentRoot "/opt/bitnami/apps/wordpress/htdocs"
#Alias /wordpress/ "/opt/bitnami/apps/wordpress/htdocs/"
#Alias /wordpress "/opt/bitnami/apps/wordpress/htdocs"
<Directory "/opt/bitnami/apps/wordpress/htdocs">
Options Indexes MultiViews +FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
RewriteEngine On
#RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
<//Directory>
# Uncomment the following lines to see your application in the root
# of your URL. This is not compatible with more than one application.
RewriteEngine On
RewriteRule ^/$ /wordpress/ [PT]
The rewrite was occurring in the last line of this file:
RewriteRule ^/$ /wordpress/ [PT]
I modified this file to the following and the redirects are working now:
# App url moved to root
DocumentRoot "/opt/bitnami/apps/wordpress/htdocs"
#Alias /wordpress/ "/opt/bitnami/apps/wordpress/htdocs/"
#Alias /wordpress "/opt/bitnami/apps/wordpress/htdocs"
<Directory "/opt/bitnami/apps/wordpress/htdocs">
Options Indexes MultiViews +FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
RewriteEngine On
#RewriteBase /wordpress/
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
<//Directory>
# Uncomment the following lines to see your application in the root
# of your URL. This is not compatible with more than one application.
#RewriteEngine On
#RewriteRule ^/$ /wordpress/ [PT]

Resources