Redirect everything BUT index.php with htaccess - apache

I have a dilemma with a site I own and how to configure the .htaccess file.
I just to had a forum in mysite.com, since I wanted to add a blog I put a 301 redirect and move everything to a dir mysite.com/forum/, all good there.
But now I want to had a different index page so people can choose between go to the blog (blogs actually) or go to the forum.
I trying to avoid losing several pages already indexed by web searchers.
Right now the htaccess file in the root of my site looks like:
Options +FollowSymLinks
RewriteEngine on
<Files 403.shtml>
order allow,deny
allow from all
</Files>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Redirigir el dominio sin www a www
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,QSA,L]
RewriteRule (.*) http://www.mysite.com/forum/$1 [R=301,L]
</IfModule>
Is there a way to put something like
If request URL = root then index.php
Else http://www.mysite.com/forum/$1
Thanks for your help
Cheers!

You probably want something like
# if request = root
RewriteRule ^/?$ /index.php [L]
# else
RewriteRule !^/?(index\.php)?$ http://www.mysite.com/forum%{REQUEST_URI} [L,R=301]
To replace the last rule that you have

Related

.htaccess doesn't redirect properly from one website to another website

I want to redirect temporarily from one URL to another URL using .htaccess
Example :
When browse from URL www.example.com, Then I want to redirect www.another-example.com. This is working good.
But don't want to redirect anywhere if browse like as www.example.com/other/path
But it is always redirecting to http://www.another-example.com if i browse like www.example.com/other/path other path.
Here is my .htaccess code
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/ [L]
RewriteRule (.*) app/$1 [L]
Redirect 302 "/" "http://www.another-example.com"
</IfModule>
How can I do it by modifying my .htaccess code?
Don't use Redirect directive as it doesn't support regex and exact matching.
Use all mod_rewrite rules as in:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/?$ http://www.another-example.com [L,R=301]
RewriteRule ^$ app/ [L]
RewriteRule (.*) app/$1 [L]
</IfModule>
^/?$ matches only landing page for current domain.
Make sure to clear your browser cache or use a different browser when testing this change.

Getting 404 error when viewing CakePHP 3 app on 1and1

When I attempt to hit my site which is at mydomain.com/subfolder, I get a 404 (from Apache).
/subfolder/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
/subfolder/webroot/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
To confirm that mod_rewrite works, I added this in the /subfolder/.htaccess file which promptly redirected:
Redirect 301 / http://other-domain-owned.com/
So, .htaccess are live and mod_rewrite works.
On my development environment, I was able to make the site work in the same /subfolder by symply enabling .htaccess files for that folder via a Directory directive.
Some tutorials say to add RewriteBase / but this didn't help.
Instead of RewriteBase, you can also use an absolute path. Additionally, the first two rules can be simplified to one
RewritRule ^(.*)$ /subfolder/webroot/$1 [L]
Similar the second part
RewriteRule ^ /subfolder/webroot/index.php [L]
Finally, Redirect and RewriteRule are from different modules. So, if one of the directives is working, this is no proof for the other one working too.

How do I use .htaccess to redirect my entire site to HTTPS apart from one specific page?

I am using a PHP framework for a new site and have recently implemented SSL. All requests are redirected to HTTPS using .htaccess which currently looks like:-
#secure htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
RewriteEngine On
#Remove index.php being displayed on pages
RewriteCond $1 !^(index\.php|images|attachments|email|css|js|robots\.txt)
RewriteRule ^/?(.*)$ /index.php?/$1 [L]
RewriteCond %{HTTPS} off
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
Everything works fine but I now need to allow access to my site from HTTP in order to get some web accessed cron tasks working. I am struggle to find how to enable mysite.com/cron to be accessed from HTTP and not to get redirected for that specific path. Can anyone help with this?
First, you need yuor redirect rule to be before your index.php routing rule. THe second thing is you can add a condition or regex so that it doesn't match /cron:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(?!cron)(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
#Remove index.php being displayed on pages
RewriteCond $1 !^(index\.php|images|attachments|email|css|js|robots\.txt)
RewriteRule ^/?(.*)$ /index.php?/$1 [L]

.htaccess old domain to a new domain not working when structure page IS NOT the same

I have this on my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ http://www.houstonhouseandhome.net/$1 [R=301,L]
</IfModule>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.houseandhomeonline\.com$ [NC]
RewriteRule ^(.*)$ http://www.houstonhouseandhome.net/ [R=301,L]
The homepage goes to the new domain, but the subpages of the old domain are not going to the index of the new domain. they go to the new domain and look for the very same page, is there a way that the requests from the old domain pages go to the new domain index?
Thank you,
For some reason you've got two sets of rules.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ http://www.houstonhouseandhome.net/$1 [R=301,L]
</IfModule>
And then
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.houseandhomeonline\.com$ [NC]
RewriteRule ^(.*)$ http://www.houstonhouseandhome.net/ [R=301,L]
You only need the first set. The [L] flag tells apache to stop processing any more rules if this one matches. Since it will match everything, the second set of rules will never take effect.
To to stop everything trying to find a relative page on the new domain, you simply remove the $1 from the end of the domain. So your htaccess file will look like this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ http://www.houstonhouseandhome.net/ [R=301,L]
</IfModule>
Note
Since you're using 301 redirects, make sure your browser has cleared its cache/history before retesting. Browsers will not request an url it thinks will respond with a 301.

Redirect subdomain to another subdomain

i want to know how could i redirect subdomain to another subdomain and also different domain name
for example
i want to redirect subdomain
forum.example.com
to
forum.example.net
thanks
This code should work for you. you need to add a redirect to your .htaccess file.
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.net/$1 [r=301,nc
Another good, but lazy way is to use the following link, to genereate the code for you htaccesss generator
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^forum\.example\.com$ [NC]
RewriteRule ^ http://forum.example.net%{REQUEST_URI} [R,L]