remove my index.php file using .htaccess - .htaccess

I am trying to remove the trailing index.php on my main site using .htaccess file and the following code....
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.(html?|php[45]?|[aj]spx?)\ HTTPS/
RewriteRule index\.(html?|php[45]?|[aj]spx?)$ https://www.gekkodev.com/%1 [R=301,L]
But of course it is still not working! I think the problem is my ssl certificate as that code has worked fine on lots of other sites
Any ideas would be greatly accepted!
Many thanks.
Phillip

Your RewriteCond is taking entirely the wrong approach. An HTTPS request is just an HTTP request wrapped in SSL/TLS security - it will not contain the string HTTPS in the request line, which is what you are checking for.
If you want the rule only to apply to HTTPS requests, just use the %{HTTPS} variable, as listed in the documenation:
RewriteCond %{HTTPS} on
(I've seen a lot of rewrite rules testing %{THE_REQUEST} recently, and I'm not sure why, as it should really only be used as a last resort when nothing else can work.)

Sorted this is how I have done it!
RewriteEngine on
RewriteCond %{HTTPS} on
RewriteRule index\.(html?|php[45]?|[aj]spx?)$ https://www.gekkodev.com/%1 [R=301,L]
Cheers IMSoP

Related

SSL .htaccess - Simple Q

I truly hate this file... I just spent 6.5 hours trying to figure this out and with my ADHD dyslexia it's just impossible!!
I have a domain that I bought for SSL for (currently I Have to wait for the ssl for WWW to kick in but for now the domain without WWW works, for example:
https://tomas.com
The .htaccess I have in root is currently:
RewriteEngine On
RewriteCond %{HTTP_HOST} tomas\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://tomas.com/$1 [R,L]
And the code above does in fact activate SSL which is good. The thing is, I have a few files in root domain but one of them called is:
hello.php (located at: "tomas.com/hello.php")
If I go to:
http://tomas.com/hello
I want it to display that file (and in address bar it should say: "http://tomas.com/hello").
Before the SSL I had this code below and it worked (but not anymore):
RewriteRule ^([^/]*)/?(.*)$ $1.php
Any idea how the entire .htaccess is supposed to look like? :/
I'm also same time trying to FORCE it to NOT use www (so if they do it should be redirected to a non WWW url)
Thank you so much in advance!!!!!!!!!!!!
Before the SSL I had this code below and it worked (but not anymore):
RewriteRule ^([^/]*)/?(.*)$ $1.php
Not sure how this "worked" before, it's not complete by itself and does more that simply append a .php extension. You need something to prevent a rewrite loop, since hello.php also matches the pattern ^([^/]*)/?(.*)$.
Try the following instead, after your HTTP to HTTPS redirect.
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule (.*) $1.php [L]
This first checks that the file with a .php file extension exists before internally rewriting to it.
Alternatively, you could instead just enable MultiViews if you aren't doing any other URL rewriting. For example, at the top of your file:
Options +MultiViews
This uses mod_negotiation to basically enable extensionless URLs for everything!

lopping .htaccess condition rule when in aws cluster

I am trying to redirect a link ONLY if it's www.domain to a subdomain admin.domain I have been looking around and I am thinking it's my syntax messing me up. I am not so great with .htaccess redirects and rewrites. Here is what I have used so far.
Example 1
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RedirectMatch 301 ^/wp-admin/?(.*)$ http://admin.mydomain.com/wp-admin/$1
Here is the second on I also have been trying.
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RewriteRule ^/wp-admin/(.*)$ http://admin.mydomain.com/wp-admin/$1 [QSA,L,R=301]
This is working off an amazon AWS cluster so the code needs to stop once it rewrites only with www. That is where the loop keeps happening.
Any help would be deeply appreciated!
I think you got it backwards, you want to redirect IF the host is www.mydomain.com, not when it ISN'T. So get rid of the !:
RewriteCond %{HTTP_HOST} ^www.mydomain.com$ [NC]
RewriteRule ^/?wp-admin/(.*)$ http://admin.mydomain.com/wp-admin/$1 [QSA,L,R=301]
Note that the first code block you have won't work at all. You're using a mod_rewrite condition with a mod_alias directive, they're completely independent of each other.

.htaccess https redirect for a single page (using relative urls)

I need to be able to redirect a single page from standard http to https. For example, I want to go from http://domain.com/quote.php to https://domain.com/quote.php.
So far I'm using this code in my .htaccess file, and it's working for the initial redirect.
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^quote.php https://domain.com/quote.php [R=301,L]
My problem, is that once I have visited the quote.php page and get redirected to the https version, all the other site pages I navigate to continue using the https protocol. This is a potential duplicate content issue, as I now have the same content accessible via http and https.
So what I want to do, is be able to do the above redirect, and then somehow do the same thing in reverse for all pages except quote.php. So if you attempted to access them via https, it would redirect to the default http version.
I use relative URLs throughout the site, so I can't simply hard-code in the https/http prefix. I need to be able to do this via .htacess, if possible.
Any help is greatly appreciated.
RewriteCond %{HTTPS} off
RewriteRule ^quote.php$ https://domain.com/quote.php [R=301,L,QSA]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/quote.php
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L,QSA]
answer to comment:
for adding new page to conndtions just put them parenthesis.like:
RewriteCond %{HTTPS} off
RewriteRule ^(quote|contact).php$ https://domain.com/$1.php [R=301,L,QSA]
question 2: QSA flag add current query string to new URL. it happens by default except in case you change query string. You can delete them now safely but if you have added query string, and wanted to have old one too, put that back.
Edit 2:
code above has a little security issue :(, actually it's more than a little :-D.
when you are using https to transfer html codes and page is using relative paths, so that's fine. but when you put these codes in .htaccess they turn into http and that's the problem:-). put the code below to sove the problem:):
RewriteCond %{HTTPS} off
RewriteRule ^(quote|contact).php$ https://domain.com/$1.php [R=301,L,QSA]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(quote|contact).php
RewriteCond %{REQUEST_URI} !^/(.*)\.(css|png|js|jpe?g|gif|bmp)$
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L,QSA]
Now, all images,scripts,.. that you are using on secure pages, are transferring securely.

.htaccess redirecting secure

Haven't found what i'm looking for so far.
I want to redirect all my site visitors to the secure version of the URL they type in, so i've put this in my .htaccess-file.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R,L]
So far so good, but now I want that visitors who type in
www.mywebsite.com/directory/param1/etcetera/ or
www.mywebsite.com/directory/file.php?param1=value1
are being redirected to
https://www.mywebsite.com/directory/param1/etcetera/ or
https://www.mywebsite.com/directory/file.php?param1=value1
as well, cause that isn't happening.
Can some guru help me out? :) thnx
Your first case, www.mywebsite.com/directory/param1/etcetera/, should be redirected to its https variant by your RewriteRule. I can't see why that wouldn't work.
To cover the second case, in order to have Apache include the query string, you need to add the QSA flag, so:
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R,L,QSA]

Re-direct problem with HTACCESS edit

RewriteEngine On
RewriteCond %{HTTP_HOST} ^tsgcs.ca$
RewriteRule (.*) http://www.tsgcs.ca/$1 [R=301,L]
This is giving me errors 'Firefox has detected that the server is redirecting the request for this address in a way that will never complete.'
Any ideas why?
You're trying to redirect the non-www version of a URL to the www version. It's almost correct, but the matching in the RewriteRule isn't quite right. Take a look at the examples here:
http://www.webweaver.nu/html-tips/web-redirection.shtml

Resources