Mod re-write on sub folder - .htaccess

I'm trying to mod re-write to a specific sub folder called /checkout this has an SSL certificate attached for https
So I have this in the root folder of my site -
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
# in http: force secure.html to https
RewriteCond %{server_port} !=443
RewriteCond $1 ^([^/]+)$ [NC]
# this kinda works below but is resolving to index2.php and is showing in the url so not really working yet
RewriteRule checkout/([^/]+)\.html$ https://mysite.com/checkout/index2.php [L]
Except that instead of showing something like
https://mysite.com/checkout/index.html
it shows
https://mysite.com/checkout/index2.php
(index2.php is a test url btw)
I've also tried amending the .htaccess in the sub folder to to something like
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{server_port} !=443
RewriteCond $1 ^([^/]+)$ [NC]
RewriteRule ^([^/\.]+)/?$ https://mysite.com/checkout/index2.php [L]
I just want to process
https://mysite.com/checkout/index.html or https://mysite.com/checkout/about-us.html as the same I would in the root like this
https://mysite.com/index.html => https://mysite.com/index.php (and then get the variables etc)
ie
RewriteRule ^/?(catalogue-page)/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+) index2.php?friendly_url=$1&catalogue_id=$2&view=catalogue&categoryid=$3&subcategoryid=$4&subjectid=$5&item=$6
I think that https is more complex.
Any pointers/help etc much appreciated

Your RewriteRule seems to indicates that you are trying to take a non-secure url and display a different page via https, this in itself is not possible. Having said that your description doesn't sound like that is the overall aim.
If you wish to force non-secure URLs to be secure, this is fine and it's also fine to want requests for index.html to server index.php. It's not however possible to do this all in one fell swoop.
# first force urls to be secure.
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^checkout/([^/\.]+)/?$ https://mysite.com/%{REQUEST_URI} [R=301,L]
# now rewrite them, so that the requests all get served through the front controller
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^checkout/([^/\.]+)/?$ /checkout/index2.php [L]

Related

.htaccess file hide GET request in URL

I am very new to .htaccess files and may have done mine wrong. I have been gathering snippets of code trying to get done what I want.
This is what I have in my file so far
I read this should be in your .htaccess file
Options +MultiViews
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
This was added to force a redirect from HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This was added to remove .php at the end of all the files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This was added to remove the showing of /index when the home link was pressed
RewriteCond %{THE_REQUEST} ^.*\/index?\ HTTP/
RewriteRule ^(.*)index\.php?$ "/$1" [R=301,L]
I have tried a lot of ideas I found on both here and other sites from Google. I have had no luck hiding the GET request.
Currently, the URL looks like https://mysite.ca/event?id=123
What I would like it to look like is either https://mysite.ca/event/123
This is hosted on Godaddy and it is just a plain PHP site if that makes any difference.
Any ideas would be great!
First of all Options +MultiViews already removes .php extension so you don't need:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
About your problem try this Rule:
RewriteRule ^event/([0-9]+)?$ event?id=$1 [NC]
Which changes url from example.com/event/<number> to example.com/event?id=<number>
Although, if your site makes infinite redirects then you should change it to:
RewriteRule ^events/([0-9]+)?$ event?id=$1 [NC]
This happens if you have some includes/headers that redirect back to event and creates infinite loop.

Setting .htaccess to go to https and php instead of html

I have my hosting with 1and1, I have just got a SSL certificate. I currently have changed the .htaccess to load .php files instead of html files which works fine with this code:
RewriteEngine on
#remove the need for .php extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Now I want the site to load with https, regardless of if it is entered like http://www or http:// etc. I am getting so many different ways to do this from various places as most same that I am supplying duplicate sites if I don't specify either http:// or http://www. So I think I will be solving 2 problems in one go. The code 1and1 supply is:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.sitename\.co.uk$ [NC]
RewriteRule ^(.*)$ https://www.sitename.co.uk/$1 [R=301,L]
But this does not seem to do much on its own? Can I conbine the 2 to simply load https://www.site for everything and change all links to load .php.
This works perfect
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteEngine on
#remove the need for .php extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

.htaccess rewrite for url parameters?

i have been trying to make my urls "pretty" / human readable, the urls at the moment are:
[BASE_URL]/?action=viewProposal&proposaltitle=tesst
I want to rewrite them to be just [BASE_URL]/tesst
I tried using the following code and modifying it but it wouldn't work, ie. it didn't redirect the pages but didn't throw any errors.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^users/(\d+)*$ ./profile.php?id=$1
RewriteRule ^threads/(\d+)*$ ./thread.php?id=$1
RewriteRule ^search/(.*)$ ./search.php?query=$1
Will the PHP GET functions still work properly as ?action defined whether its a view / edit / delete?
I am assuming base url your index.php try following rule,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)$ index.php?action=viewProposal&proposaltitle=$1 [QSA,L]

htaccess redirect not working properly

I am trying to accomplish the following but my code is not working
1) I want to do either non-www to www or www to non-www
2) desktop users should be redirected to example.com/homepage
3) remove or hide /homepage from url
4) redirect mobile users to example.com/m
here is my code, it's not working, too many problems to list, but off the top of my head, I can think of 2 problems that I encountered: "cannot open page because too many redirects occurred" and mobile users are taken to /homepage instead of /m
# remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]
RewriteEngine On
RewriteRule ^$ /homepage [L]
RewriteEngine On
RewriteRule ^$ homepage/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ homepage/$1
RewriteCond %{HTTP_USER_AGENT} ^.*(ip(ad|od|hone)|blackberry|iemobile|android).*$ [NC]
RewriteCond %{REQUEST_URI} !^/m/.*
RewriteRule ^.*$ http://example.com/m [R=301,L]
Please help me, I am so frustrated, what am I supposed to change about this code to get it to work?
Unfortunately im no genious with regex or redirects in htacces but I have gotten similar code to work myself.
It does sound like your code is getting stuck in a redirect loop and the browser is stopping it from continuing.
I know thats not a complete answer but Hopefully that helps you make more sense of the code atleast a little.
Hopefully someone else can chime in otherwise i can dig up the code that is working when im on the computer and hopefully give you a better response.
-Sent from my cell
The www/non-www is fine.
For the homepage stuff, you need to do two things: make sure what you're rewriting into the homepage directory actually exists (otherwise you get a 500 server error) and exclude mobile user agents.
So this stuff:
RewriteEngine On
RewriteRule ^$ /homepage [L]
RewriteEngine On
RewriteRule ^$ homepage/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ homepage/$1
Needs to look like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_USER_AGENT} !^.*(ip(ad|od|hone)|blackberry|iemobile|android).*$ [NC]
RewriteCond %{DOCUMENT_ROOT}/homepage%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/homepage%{REQUEST_URI} -d [OR]
RewriteRule ^(.*)$ homepage/$1 [L]

.htaccess url rewriting - exceptions for certain files only

This is my .htaccess:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteCond %{REQUEST_FILENAME} !\.php$ [NC]
RewriteRule ^([^/]+)/?$ index.php?page=$1 [QSA]
Basically if you enter /test, it will work perfectly, and if you go to /test.php, it will show the PHP page without the rewrite rule, which is what I need for certain pages only.
let's say there are some pages (a few) where I would want this to be able (to load the whole .php page), but ALL the other pages, what I want is that if someone goes to /contact.php, it redirects to /contact, to the right one.
So let's say for "result.php" and "search.php", I want it to be able to load the whole PHP page, but for the rest, I want it to redirect to the right adress (so /contact.php => /contact).
How could I do it? Basically I need to add this RewriteRule ( RewriteCond %{REQUEST_FILENAME} !\.php$ [NC]) only for a few pages (that I would determine myself), and for the rest of the pages I need another RewriteCond (that redirects all the "test.php" to "test")
Thanks a lot!
You can put all those exception in your first RewriteCond and alter 2nd RewriteCond to skip real files and real directories like this:
RewriteCond %{REQUEST_URI} !/(index|result|search)\.php [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?page=$1 [QSA,L]

Resources