I have two separate .htaccess files that I'd like to be fused together so that the first rewrite always takes precedence, which redirects some traffic to https base which domain. Then if the file at the url does not exist, then it sends the traffic to a php file. It is a url shortener, but if that returns at 404, then it shows a 404 error page.
Here are the bits and pieces of the .htaccess files:
This, below, I believe should redirect all http traffic except kore.tt and korett.com to https:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(www\.)?(kore\.tt|korett\.com) [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
This is something from the url shortener that is supposed to send traffic that doesn't exist to loader.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /loader.php [L]
</IfModule>
But then if that returns a 404 error. Then this is the simple 404 error catch.
ErrorDocument 404 /404.html
You can use a condition in your htaccess. There are many more ways to create rules. See if this can work for you.
#check to see if loader.php exists on the filesystem, then do rewrite
<If "-f %{DOCUMENT_ROOT} . '/loader.php'">
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /loader.php [L]
</If>
#otherwise redirect to 404 page
<Else>
RewriteEngine On
RewriteRule ^ /404.html [R=404,L]
</Else>
Related
I have two domains pointing to the same place but I only want one of these to redirect all http requests to https.
http://www.thisisme.com should stay as it is
http://thisisme.mydomain.com needs to redirect to https://thisisme.mydomain.com
As an extra rule I need to make sure that http://thisisme.mydomain.com/health_check.php does not redirect.
Here's what I have so far.
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# Don't redirect /heath_check.php to https
RewriteCond %{REQUEST_URI} !(health_check\.php)$
#This line needs looking at! Should only redirect any xxx.mydomain.com requests to https
RewriteCond %{HTTP_HOST} mydomain\.com
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# block hidden directories
RewriteRule "(^|/)\." - [F]
# This makes the url look pretty for codeigniter...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
</IfModule>
EDIT!
I actually need to do the opposite if the requested domain is not mydomain.com - it needs to redirect to http is https is requested
To exclude www.thisisme.com ,put the following condition right bellow RewriteEngine on line or above the http=>https rule :
RewriteCond %{HTTP_HOST} !^(www\.)?thisisme\.com$ [NC]
My htaccess files contains only a few lines that firstly remove the www and then add ".php" to the slug to get the correct php file, so
www.kalicup.fr/seo
should rewrite to
kalicup.fr/seo
and then display the file seo.php (without the .php extension displaying in the url itself)
at the moment
kalicup.fr/seo
correctly displays seo.php without showing the file extension.
however, when I try
www.kalicup.fr/seo
it rewrites to
kalicup.fr/seo.php
adding the .php extension in the url
so there's abviously a problem in my htaccess but I can't see it !
here's my code
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
ErrorDocument 404 /404.php
# redirect the url with www to url without
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?kalicup\.fr)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?kalicup\.co\.uk)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
# add .php to urls
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
can anyone see the problem ?
Use that in your .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
ErrorDocument 404 /404.php
# redirect the url with www to url without
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?kalicup\.(?:fr|co\.uk))$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
# add .php to urls
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Only one test for .fr and .co.uk.
And -MultiViews: http://httpd.apache.org/docs/2.2/en/mod/core.html#options
The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements.
http://httpd.apache.org/docs/2.2/en/content-negotiation.html
I have my site as HTTPS "https://website.com", so I added a htaccess file to redirect to https when I access the website through HTTP.
Here's the code that I got from SO.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
When I access http://website.com, it redirects to https://www.website.com which is correct. But when I accessed http://www.website.com, it redirects to https://www.website.com/index.php
There's an index.php in the url which I do not like. Are there any possible solution for this?
Have your code like this:
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
You don't need multiple RewriteEngine On lines and keep redirect rule before internal rewrote rule as shown.
Make sure to clear your browser cache before testing.
I'm setting up a small set of rewrite rules in an htaccess file, where I want every url to go to a index.php file except for /admin which I want to redirect to admin.php. Not very familiar with mod_rewrite or regexp unfortunately.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin$ admin.php [L]
RewriteRule . /index.php [L]
</IfModule>
This gives me an internal server error (not saying 500). Removing or uncommenting the admin rewrite makes it work.
The conditions need to be applied to the index.php rewrite rule, otherwise it causes a redirect loop. A RewriteCond only gets applied to the immediately following RewriteRule so the rule that routes everything to index has no conditions. Try just rearranging the lines:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^admin$ admin.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I have an already ok htaccess script that is designed to forward /about to /about.php without displaying it, but I was wondering if anyone knew how to modify my .htaccess file to to forward requests to /about.html to /about (using 302 rather than a hidden redirect)
CheckSpelling on
Options -Indexes
Options +FollowSymlinks
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?page=$1 [L,QSA]
try
RewriteCond %{REQUEST_URI} ^/(.+)\.html$ [NC]
RewriteRule . /%1 [R=302,L]