.htaccess Subdirectory Rewrite with Jekyll - apache

I have a small blog in generated with Jekyll up on a shared Apache server. I have been following this guide to set up the server so that I can update the site by pushing changes via GitHub, but I haven't even gotten that far yet. The .htaccess rewrite rule to point the domain to the generated /_site subdirectory has me stumped. The code I'm using (below) is redirecting the site correctly, but the guides I've read say that this code should hide the subdirectory from the URL, and this is not happening. It works correctly on the homepage, but subpages still have /_site/ in their URL. Any ideas?
My website
.htaccess file
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?joejoiner.tk$ [NC]
RewriteCond %{REQUEST_URI} !^/_site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /_site/$1
RewriteCond %{HTTP_HOST} ^(www.)?joejoiner.tk$ [NC]
RewriteRule ^(/)?$ _site/index.html [L]

Add a trailing / to the end of line 6 in the above code block, like so:
RewriteRule ^(.*)$ /_site/$1/

Related

.htaccess redirect issue with multiple .htaccess file

I am using .htaccess file like below for redirect www to non www on my public_html directory
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
its working fine and redirect like below
www.example.com
to
https://example.com
Now in my sub directory called latest, I have another .htaccess file like below
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
which is making my url clean like below
www.example.com/latest/index.php?page=1
to
www.example.com/latest/1
but even I have .htaccess file in home directory which is making www to non www, my this directory does not redirect www to non www.
I want redirect
www.example.com/latest/1
to
https://example.com/latest/1
I am sure .htaccess file in my directory is causing issue but I do not know how to resolve it. Let me know if anyone here can help me for the same.
Thanks!
The problem is, mod_rewrite is not inherited (at least by default). If you have a rewrite rule in the "default" directory .. You're going to have to include that rule in all subsequent directories "below" that. I have never seen (or heard of) a setting for allowing mod_rewrite to allow inheritance from parent .htaccess files. In general, this is just accepted as "the way it is".
UPDATE
THIS QUESTION In Server Fault explains this as well. The mod_rewrite rules from the previous htaccess files don't even get processed.
COMBINE THE 2 in the SUB DIRECTORIES
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# add this only to sub directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]

Flask - CGI - need to fix issues with htaccess

So I have successfully deployed a Flask app using CGI. In order to get the app working, my .htaccess file had to look like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /home/user/public_html/mysite/cgi-bin/main.cgi/$1 [L]
However, I am facing a couple of issues.
mysite.com works, but when I link other pages to the home page, it takes them to mysite.com/cgi-bin/main.cgi. I'd like for the links to take users to mysite.com. Similarly, when I try to link to another page, it goes to mysite.com/cgi-bin/main.cgi/page2, when I actually want it to be mysite.com/page2. How can I fix this?
The following .htaccess content seems to not work:
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
Header always set Content-Security-Policy "upgrade-insecure-requests;"
I'm not sure if the above two issues are related, but I'd like to fix both.
With your shown samples, please try following Rules. Please make sure your htaccess Rules file is besides your cgi-bin directory/folder. Also clear your browser cache before testing your URLs.
RewriteEngine ON
##Rules for applying https to urls.
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
##Rules to remove www from urls.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule ^ https://%1/%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cgi-bin/main.cgi/page2/?$ cgi-bin/templates/page2/index.html [NC,L]
JS/CS rewrite/redirect:
You may need to use base tag to fix your js and other relative resources. If you are linking js files using a relative path then the file will obviously get a 404 because its looking for URL path. for example if the URL path is /file/ instead of file.html then your relative resources are loading from /file/ which is not a directory but rewritten html file. To fix this make your links absolute or use base tag. In the header of your webpage add this <base href="/"> so that your relative links can load from the correct location.

Endless loop in .htaccess when rewrite to a folder

I've got the root of my domain with some folders.
app
scripts
shop
And I want to redirect with the .htaccess everything to the folder shop, except app and scripts. I wrote the next .htaccess, but I've got a 500 ERROR INTERNAL SERVER. I saw the log file and it says that there is and endless loop.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/app
RewriteCond %{REQUEST_URI} !^/scripts
RewriteRule (.*)$ shop/$1 [L]
I have another hosting with other apps and cms in the same hosting provider. Even with the same structure and it works perfect, then I don't undertand why doesn't work here.
How can I avoid the endless loop?
EDIT: I want that the redirect will be invisible, not with [R]
EDIT 2: My purpose is:
Having this folders:
app
scripts
shop
Access to the shop folder with the URL: domain.com
Access to the app folder with: domain.com/app
Access to the scripts folder with: domain.com/scripts
You'll also have to exclude URLs beginning with shop:
RewriteEngine On
RewriteRule ^/?$ /shop/ [L]
RewriteCond %{THE_REQUEST} ^GET\ /shop(/\S*)? [NC]
RewriteRule ^shop / [R=301,L,NC]
RewriteCond %{REQUEST_URI} !^/(app|scripts|shop)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /shop/$1 [L]

https to http htaccess redirect not working when SSL enabled

I'm trying to redirect http to https on all pages via an htaccess in the main root of the website (shared hosting). I can't understand why this isn't working? This is the entire content of the htaccess file. Whilst there seem to be similar questions already asked on Stackoverflow, I can't find any solutions that would seem to be of help.
There is one folder/directory on the website that requires SSL so I can't disable SSL on the whole website. That folder has its own htaccess file.
The second chunk of code is very standard as it's a Joomla website. I can't imagine the issue would be in there?
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
##### Joomla! core SEF Section -- BEGIN
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|raw|ini|zip|json|file|vcf))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
##### Joomla! core SEF Section -- END
If you use joomla first disable ssl, than use above code because I have use this code also and its working.
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

CodeIgniter index.php removal htaccess causing mis-redirection

I'm using following htaccess rule which is proposed all over internet for removing index.php in codeigniter urls. And there are some of the redirection rules i added above it.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]
The problem is:
I'm getting some odd http request, which i think is caused by htaccess rules above.
Here are some of them :
https://www.sitename.com/index.php/favicon.ico
which ought to be .com/favicon.ico
https://www.sitename.com/index.php/scripts/jquery.js
which ought to be .com/public/jquery.js
as a side note im using base tag to redirect assets to /public/
strange thing is i couldnt find where the second redirection happens
i tested whole site and javascript & css files load correctly
i handled this redirections by making my controller ignore those requests but a while ago
a strange error happened. which i asked here:
https://stackoverflow.com/questions/11775849/htaccess-didnt-work-until-renaming-and-then-renaming-back
my guess is that, even though i ignore misredirected request, hosting company receives them and probably it was causing some trouble for them which led to the problem i shared in linked question by a maintenance of hosting company.
Anyway, Question is:
How can i make htaccess rule only redirect requests that doesnt have file extension at the end?
You're missing a line before the REQUEST_FILENAME line:
RewriteCond $1 !^(index\.php|assets|something|fav\.ico|robots\.txt)
You can modify this to suit your needs. I've got my JS/CSS etc in the assets folder so it's not affected by the rewrite.
so the full .htaccess will look like this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond $1 !^(index\.php|assets|something|fav\.ico|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]