Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+2 votes
1.2k views
in Q2A Core by

I am already redirecting www.example.com/ to http://example.com/ through .htaccess in http://example.com/.  But how do I redirect www.example.com/q2a/ to http://example.com/q2a/  and apply it to the questions (subdirectories too?

I have already tried this:  http://www.question2answer.org/qa/26368/how-can-i-redirect-www-name-com-to-name-com

It either does not work (leaves the www) or (I think) it throws a 404 error because it redirects to the home page http://example.com/.

Just in case, these are the contents of http://example.com/.htaccess  :

DirectoryIndex index.html
RewriteEngine On
# remove the www
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
#
# remove the index.html from the home page
RewriteCond %{THE_REQUEST} \/index.html\ HTTP [NC]
RewriteRule (.*)index.html$ /$1 [R=301,L]

# redirect known bad urls (404 errors) to the correct page

# redirect all other bad urls (404 errors) to home page
ErrorDocument 404 http://example.com

########## Begin - Compress output
<IfModule mod_deflate.c>
<FilesMatch "\.(js|css|htm|html)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html
########## End - Compress output

 

Q2A version: 1.6.3
by
Came on.  Is it so difficult?  

Maybe I have to merge the  http://example.com/  and  http://example.com/q2a/  .htaccess files?
by
I just tried the solution given by rahularyan.com.  It did not work for me.

>>>>
 For www to non-www

DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
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]


# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

</IfModule>
<<<<<<<<<<<

1 Answer

+1 vote
by
This is the htaccess I am using for a long time and it is working:

 

RewriteOptions inherit
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /

# remove the www
RewriteCond %{HTTP_HOST} ^(www\.example\.com)?$
RewriteRule ^(.*)$ http://example.com/q2a/$1 [R=301,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>
...