.htaccess remove .html file/directory priority rewrite - regex

I'm trying to rewrite so you can access files without .html and without adding a trailing slash. here's the code being used:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.html -f
RewriteRule ^(.*?)/?$ $1.html
But for my file structure on the server where I have this:
/public_html
..
/beginners
beginners.html
and inside /beginners folder there's other files, e.g.
/beginners/page1.html
I need rewrites to work like this:
if user inputs url:
website.com/beginners - the server would return the contents of
file beginners.html currently it returns the contents of the directory /beginners . so i need the server to first check if beginners.html exists, if yes - then server serves beginners.html not the directory /beginners
if the user accesses url website.com/beginners/page1 the server should first check up if
page1.html exists in the folder beginners and if it finds the file page1.html then it serves the contents of the file page1.html
how can this be done?

Here's the .htaccess that does it:
Options +FollowSymLinks -MultiViews -Indexes
DirectorySlash Off
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.html -f
RewriteRule ^(.*?)/?$ $1.html

Related

Use .htaccess to redirect to directory URL but show the URL as if in root

I currently have a CRUD that has the following file structure:
The index.php file redirects to the add-product.php, delete.php and other files. The problem is, the URL looks like: myapp.com/resources/views/add-product but I want it to look like myapp.com/add-product. I found other StackOverflow posts similar to this, but it redirects to the desired link, instead of redirecting to the correct link but showing the desired one, and because of that the site doesn't work ("The requested URL was not found on this server"). The .htaccess file looks like this:
Options +FollowSymLinks +MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^resources/(.*)$ /$1 [L,NC,R]
I'm also using the file to be able to remove the .php from the URLs. The last line is the one handling the wrong redirection. How can I go about this? Thank you for your time.
You may use it like this:
Options +FollowSymLinks +MultiViews
RewriteEngine On
RewriteBase /myapp/
# external redirect to remove /resources/views/ from URLs
RewriteRule ^resources/views/(.+)\.php$ $1 [L,NC,R=302]
# internal rewrite to add /resources/views/ and .php to show content
RewriteCond %{DOCUMENT_ROOT}/myapp/resources/views/$1.php -f
RewriteRule ^(.+?)/?$ resources/views/$1.php [END]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

.htaccess file blocks css, js but how?

I have a .htaccess file inside an existing project folder. The code for the .htaccess file is below.
# Use PHP54 Single php.ini as default
#Options +FollowSymLinks
#Options +Indexes
RewriteEngine On
RewriteBase /
#RewriteRule ^users/([A-Za-z0-9_]+)$ users/index.php [NC,L]
#RewriteRule ^([A-Za-z0-9_]+)$ users/$1 [NC,L]
#RewriteRule ^ - [L]
#RewriteCond %{DOCUMENT_ROOT}/users/$1 -f
#RewriteRule .* users/$1 [L]
#Options -Indexes
#RewriteEngine On
#RewriteRule ^[!/.]*([A-Za-z0-9]+)/?$ /index.php?id=$1 [NC,L]
#<FilesMatch "\.(?i:vcf)$">
# ForceType application/octet-stream
# Header set Content-Disposition attachment
#</FilesMatch>
# If requested resource exists as a file or directory, skip next two rules
RewriteCond %{DOCUMENT_ROOT}/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule (.*) - [S=2]
#
# Requested resource does not exist, do rewrite if it exists in /users
RewriteCond %{DOCUMENT_ROOT}/users/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/users/$1 -d
RewriteRule (.*) users/$1 [L]
#
# Else rewrite requests for non-existent resources to /index.php
RewriteRule (.*) users/index.php?q=$1 [L]
So what happens it, when I try to open the project in url at localhost
http://localhost/projectfolder
it opens the index file without any javascript and css. Also the links to other pages do not open and the error message shows as -"Object not found". So I can only view index.php with out css and js files.
But when I remove / rename the .htaccess file, all works fine. So there must be something in this file which blocks all css, js files. So I am not asking for all the rules of writing a .htaccess file, I just need to know which line of code is blocking the files and how can I edit them, so it will not block any file wile working in localhost.
Note:- This question I am asking only for my knowledge. As I have written, if I remove the file then its working fine. But still I want to know which line of code is blocking the access to files. Thanks.
I suspect this rule is not doing what you think it should be doing:
RewriteCond %{DOCUMENT_ROOT}/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule (.*) - [S=2]
Replace this with:
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
Also you might be using relative links for css/js/images which will also cause 404 after rewriting to /users/. You have 2 options to fix that:
use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
You can try adding this in your page's HTML header:
<base href="/" />

.htaccess access to web root and sub-folders

I have the .htaccess in the main folder (/public_html) with the following lines in it. These are for an application i have installed on my domain.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
It seems like this rule makes every sub-folders inaccessible. For example i have a sub folder called /public_htm/public. I want this sub-folder and all of it's contents to be accessible to public. If i put a .htaccess file in this subfolder, what lines it needs to have to give access to it's content?
Replace your .htaccess with this code:
Options +FollowSymLinks -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ app/webroot/$1 [L]
</IfModule>

mod rewrite for pretty urls and also hide index.php

How can i mod-rewrite to obtaining a pretty url like below
example.com
contains a form to get a text value and pass it another file for processing that file is in directory as web but i need to change that as pretty urls
example.com/web/index.php?url=mydomain.com
to
example.com/web/mydomain.com
example.com/web/newdomain.com
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 %{THE_REQUEST} ^[A-Z]{3,}\s/+web/index\.php\?url=([^\s&]+) [NC]
RewriteRule ^ /web/%1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^web/(.+?)/?$ /web/index.php?url=$1 [L,QSA]

append path directory to url using htaccess

how could i get Apache to redirect
http://localhost/index.php
http://localhost/create/index.php
http://localhost/create/contact.php
http://localhost/engage/page1/services.php
to
http://localhost/Project1/index.php
http://localhost/Project1/create/index.php
http://localhost/Project1/create/contact.php
http://localhost/Project1/engage/page1/services.php
respectively?
Essentially I need to append "Project1" (or whatever other string I see fit) to the BEGINNING of the url path
Thanks
You can use negative lookahead like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# redirect to /Project1/ if it is not already /Project1/
RewriteRule ^((?!Project1/).*)$ Project1//$1 [L,NC]