Configure htaccess to ignore anything after last slash - .htaccess

i removed images extension from URL :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.png -f
RewriteRule ^(.*)$ $1.png [NC,L]
but i need to ignore anything after images URL for ex :
if i add a random after last slash images url :
mysite/images/imagename/z54zrg89zgz98gzg
the images must be work.
i tried this line but doesn't work :
RewriteRule mysite/images/(*)/^ - [L]

You can use:
RewriteEngine on
# For /images/ directory:
RewriteRule ^images/([^/]+)(?:/.*)$ images/$1.png [NC,L]
# For others (If necessary)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.png -f
RewriteRule ^(.*)$ $1.png [NC,L]

Related

How to remove the need of txt extension with a final optional trailing slash in url using htaccess

How to remove the need of txt extension of txt files and add a final optional trailing slash in url using htaccess regex in php
my code is
#turn on url rewriting
RewriteEngine on
#remove the need for .txt extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.txt -f
RewriteRule ^(.*)(/?)$ $1.txt
But it throws 500 Server error.What's wrong with the code?
Based on your shown samples, could you please try following, please make sure you clear your browser cache before testing your URLs. Fair warning I haven't tested it as of now.
RewriteEngine ON
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/$1 !-d
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{DOCUMENT_ROOT}/$1\.txt -f
RewriteRule ^(.*)/?$ $1.txt [L]
EDIT: As per OP's comments adding following solution.
RewriteEngine ON
RewriteBase /
RewriteCond %{REQUEST_URI} /$
RewriteRule ^(.*)/$ $1 [R=301,L]
RewriteCond %{DOCUMENT_ROOT}/$1 !-d
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{DOCUMENT_ROOT}/$1\.txt -f
RewriteRule ^(.*)/?$ $1.txt [L]

.htaccess "Internal Server Error" when adding / to URL

I've got the following .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^profile/([A-Za-z]+) profile.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
But you can't add a / to the end of the URL. For example if you goto something it works but something/ doesn't. So how do I allow a / to be added to the URL?
Here is a possible solution if anyone else is having the same problem (not fully tested but seems to work).
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^profile/([A-Za-z]+) profile.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*?)(/.*)?$ $1.php [NC,L]
To allow the trailing slash in URLs that point to your php files, replace your last rule with this :
RewriteRule ^(.*)/?$ $1.php [NC,L]
/? matches the slash as an optional character so your rule will also match URIs not ending with a / .

Rewrite url - Rediriger sans index.php

my URL is of the type: www.mysite.fr and for any page: www.mysite.fr/index.php/test
I wish that www.mysite.fr/test displays www.mysite.fr/index.php/test (it's more beautiful without index.php!)
I tried this but: www.mysite.fr/test displays the home page instead of the test page
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Try this :
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,7}\s/(.*)index\.php/(.*)\sHTTP.*$ [NC]
RewriteRule ^ /%1%2 [R=301,L]
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule ^(.*)$ index.php/$1 [L]
Second line to remove index.php exteranlly.
Fifth line to redirect request to original path internally .
Note: clear browser cache the test
Your method works on my localhost but not on my online site. This is the 5th line that blocks in my opinion but I do not see why even adding a slash before index.
Someone have an idea ?

Need help for .htaccess RewriteRule

I've been trying to setup a RewriteRule for my .htaccess to redirect any not found .php url to my loader.php ... For some reasons, it's redirecting even if the file exist.
Here's what I have:
# Engine On for Extension Removal
RewriteEngine On
# Rewrite the url for the login page
RewriteRule ^login/?$ index.php?tab=login [NC,L]
# Load the url with the page loader if the url doesn't match any of the rule above
RewriteRule ^(.*)\.php$ loader.php?page=$1 [NC,L]
# Remove PHP Extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Everything is working before I added this line:
RewriteRule ^(.*)\.php$ loader.php?page=$1 [NC,L]
Right now, no matter what is in my link, even if it exist or not, it'll load the loader.php. And I have other rules like the login one, same thing but different name which is why I didn't bother posting them.
Try
# Engine On for Extension Removal
RewriteEngine On
# Rewrite the url for the login page
RewriteRule ^login/?$ index.php?tab=login [NC,L]
# Remove PHP Extensio
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.php$ loader.php?page=$1 [NC,L]
The problem with
RewriteRule ^(.*)\.php$ loader.php?page=$1 [NC,L]
is that it accepts any .php url in the request and rewrites it to loader.php.
The condition
RewriteCond %{REQUEST_FILENAME} !-f
change this behaviour of the rule, it tells server to process the rule only when there is a request for non existent php file.

Looking for advice on mod_rewrite and htaccess

I am not experienced with htaccess... But I am authoring a site on which I am using a simple redirect. I basically just copied the first htaccess that I found and it works fine, but I am curious if I am using it properly and/or if it is responsible for some problems I am having with the site.
This is my file..
RewriteEngine On
Exclude some directories from URI rewriting
RewriteRule ^(dir1|dir2|dir3) - [L]
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /src/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/src/.*$
RewriteRule ^(.*)$ /src/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^src/.*$ /src/index.php [NC,L]
The index.php (as you can tell) lives in /src), as does most pf the site.
One problem I am having is that accessing some of the includes results in pulling back the index.php!
Any advice, or directions to some tuts or articles on htaccess would be GREATLY appreciated.
Thanks
Have to say that the lot of the code in here looks redundant. Replace your code with this:
RewriteEngine On
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} ^/(dir1|dir2|dir3) [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} !^/src/.*$
RewriteRule ^(.*)$ /src/$1 [L]
RewriteRule ^src/.*$ /src/index.php [NC,L]
Also when you say that ccessing some of the includes results in pulling back the index.php! I hope you are just including them like:
include('path/to/somefile.php');
That won't go through .htaccess since this inclusion is transparent to Apache.
Try combining a few of the rules:
RewriteEngine On
# Exclude some directories from URI rewriting
RewriteRule ^(dir1|dir2|dir3) - [L]
# block access to the htaccess file
RewriteRule ^\.htaccess$ - [F,L]
# route "/" requests to index.php
RewriteRule ^$ /src/index.php [L]
# route requests that exists in /src/ to /src/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/src%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/src%{REQUEST_URI} -d
RewriteRule ^(.*)$ /src/$1 [L]
# route everything else to /src/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /src/index.php [L]

Resources