RewriteCond subdirectory is not working - php

I am trying to do RewriteRule for subdirectory and I could not do it.
This is the code:
RewriteCond %{REQUEST_URI} !^/subfolder
RewriteCond %{DOCUMENT_ROOT}/subfolder%{REQUEST_URI} !-f
I use the following code with the above as well to remove the .php extension and add a trailing slash at the end.
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
I can access any page except index.php
I need someone to help me fix this issue so I can access the pages/files in the subdirectory.
Thank you

You need to explicitly check if the php file exists before you add the .php extension. And remember that rewrite conditions only apply to the immediately following rule, so you need to duplicate them if they need to apply to multiple rules. Also, you want all your redirects to happen before any routing or rewrites:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/$ $1.php [L]
RewriteCond %{DOCUMENT_ROOT}/$1/$2.php -f
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
# whatever subfolder rewrites you need can go here

Related

How to exclude a subfolder rule from this Rewrite condition of htaccess

I'm trying to exclude "test" subfolder from following code i.e. "www.abcwebsite.com/test"
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
So when I create a subfolder in my directory it takes me back to "www.abcwebsite.com/404".
What I Want it to be like:
www.abcwebsite.com/file.php > www.abcwebsite.com/file should work
And also
www.abcwebsite.com/test (test being subfolder) should also work.
Thanks in Advance.

HTACCESS no php or /

I am using the following htaccess code. However this code means that my images or CSS are not loading, on the plus side it means that .php or the trailing / are needed like my client has asked for.
How can I make this work so that the images and the CSS and JS etc load correctly?
I would like the url to look like www.example.com/services or www.example.com/services/
currently google shows up both
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
# enforce a trailing-slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
# remove php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L,QSA]
See also
Htaccess: add/remove trailing slash from URL
Remove .php extension with .htaccess

Remove .php from URL - Specific case

This is my .htaccess file, everything works so far but I can't manage to remove .php extension from files, every code that I tried from other answers just threw 500 or 404 error. Please advise where and what to add. Structure of the folders is localhost/myfolder/somefile.php
Just to be clear - localhost/myfolder/ is a root for my project.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /myfolder/$1/ [L,R=301]
RewriteRule ^team-news/([0-9]+[/])?$ posts.php?p=$1&cat=Team\ News
RewriteRule ^product-news/([0-9]+[/])?$ posts.php? p=$1&cat=Product\ News
RewriteRule ^member-specials/([0-9]+[/])?$ posts.php?p=$1&cat=Member\ Specials
RewriteRule ^ambassador-blogs/([0-9]+[/])?$ posts.php?p=$1&cat=Ambassador\ Blogs
RewriteRule ^user/([0-9]+[/])?$ profile.php?id=$1
RewriteRule ^browse-all/([0-9]+[/])?$ searchall.php?p=$1
RewriteRule ^edit/([0-9]+[/])?$ edit.php?id=$1
RewriteRule ^articles/([0-9]+[/])?$ post.php?id=$1
This snippet will allow you to rewrite to remove php extensions:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
If you want your URL to have a trailing /, you can use this snippet:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Source
Removing extension, say; php or html in browser will let browser find it a little bit more effort to find the source file. if you need it so, this follows may help you:
UPDATED:
PHP:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/$ /$1.php
HTML:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)/$ /$1.html
those will remove all extensions in your files (both php and html).
Note: see if server enables mod rewrite module/extension.
You should be using two .htaccess files. The first should go in your localhost root (to redirect requests to myfolder), and the second should go into myfolder (to match up routes against your PHP files):
Root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /myfolder/$1/ [L,R=301]
myfolder .htaccess:
RewriteEngine On
RewriteBase /myfolder/
RewriteRule ^team-news/([0-9]+[/])?$ posts.php?p=$1&cat=Team\ News [L]
RewriteRule ^product-news/([0-9]+[/])?$ posts.php?p=$1&cat=Product\ News [L]
RewriteRule ^member-specials/([0-9]+[/])?$ posts.php?p=$1&cat=Member\ Specials [L]
RewriteRule ^ambassador-blogs/([0-9]+[/])?$ posts.php?p=$1&cat=Ambassador\ Blogs [L]
RewriteRule ^user/([0-9]+[/])?$ profile.php?id=$1 [L]
RewriteRule ^browse-all/([0-9]+[/])?$ searchall.php?p=$1 [L]
RewriteRule ^edit/([0-9]+[/])?$ edit.php?id=$1 [L]
RewriteRule ^articles/([0-9]+[/])?$ post.php?id=$1 [L]
Note how I have also included [L] to stop it from doing anything else once it has found a match.
Just below your 301 rule add this rule:
RewriteEngine On
RewriteBase /myfolder/
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=302,L,NE]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

Multiple RewriteRules & RewriteConds interfering

So I have the following piece of code in my .htaccess:
RewriteEngine On
# Remove .php from URL and add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301]
# Rewriting data from URL
RewriteRule ^minecraft/(.*)/$ /minecraft/$1/ [NC,L]
RewriteRule ^game/(.*)/$ /game/$1/ [NC,L]
RewriteRule ^tool/(.*)/$ /tool/$1/ [NC,L]
# Redirection if file or folder doesn't exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ ./router.php?r=/$1 [QSA,R,L]
I want to achieve the following:
A the user can visit "domain.com/contact/", which will show them the contents of the file "contact.php" in the root directory. (Notice the trailing slash in the URL)
The user can visit "domain.com/random-id/" and that request will be routed to "router.php?r=random-id".
The user can visit "domain.com/minecraft/random-name/", and that request will be routed to the "minecraft/random-name/"-folder. (With an index.php etc inside)
I cannot seem to figure it out, any help is appreciated!
Have it this way:
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301]
# Remove .php from URL and add trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
# Redirection if file or folder doesn't exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ router.php?r=/$1 [QSA,L]

Use .htaccess to remove file extensions but also allow set a homepage

I am having a problem using the .htaccess file. I have created the file and set it up so that I don't have to type file extensions in the URL. Unfortunately, this has created a new problem. I used to be able to type localhost/project and it would display index.php. But now if I type localhost/project it displays a Server Error 500. Does anyone know how I can solve this error?
.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
You need to also check the !-d with the request filename, may as well add all the right conditions:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
The problem is that /project is a directory, and mod_dir automatically redirects that to /project/, and that matches the first rule, so it turns to /project.php, which isn't a file.

Categories