htaccess - URL rewrite - Remove slashes, but not from files - .htaccess

I use PHP.
A working htaccess-file
RewriteEngine On
RewriteBase /folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule .*[^/]$ $0/ [L,R=301]
RewriteCond $1 !^(index\.php)
RewriteRule ^(.+)$ index.php/$1 [L]
Look at the row with a # comment. When uncommented it adds a redirect to a slash. I use URL rewrite with toroPHP.
I want to rewrite to ending slash
I want to redirect to ending slash from rewritten URLs, just like the code above.
I don't want ending slash from real files, like jquery.js, style.css.
Example (updated 2012-12-21)
/category/test should be /category/test/
http://www.test.com/myjsfile.js should be http://www.test.com/myjsfile.js
Problem
If I use the code above uncommented it add an ending slash to all urls, including javascript files and css files.
I only want the rewritten urls to end with slash.
Question
Can it be done with htaccess? If so how?

The htaccess was almost correct. However the REQUEST_FILENAME needs to be before EVERY rewrite rule, not just before the first.
This works
RewriteEngine On
RewriteBase /folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*[^/]$ $0/ [L,R=301]
RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [L]

Related

Allowing a trailing slash for a specific dynamic url?

I have done a 301 redirect for all trailing splash URLs in my htaccess file. Now I have a few dynamic URLs `website.com/item/activate/#dynamiclychangingvalue**/** that need the splash. Note that in the end on the activate folder is a dynamic url that needs the splash.
How would I edit the .htaccess file? I have tried all sorts of codes, they dont seem to be working.
IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
the dynamic url looks like this website.com/item/activate/#dynamiclychangingvalue**/**
You can use:
RewriteCond %{REQUEST_URI} !^/additem/ [NC]
RewriteRule ^(.+)/$ $1 [R=301,L]

Redirecting dynamic url to simple URL

I'm using CodeIgniter and trying to redirect this URL
SERVERNAME/index.php/category/category?ss=ABC to SERVERNAME/ABC
What I am doing
RewriteRule ^(.*)/$ /index.php/category/category?ss=$1 [L]
You need to add a couple of conditions so that you don't blindly rewrite everything, then you need to make the slash optional and the .* non-greedy:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)/?$ /index.php/category/category?ss=$1 [L]

htaccess: Remove .php extension

i have a file named Show.php
i want to remove .php extension of this, and if someone requested /Show.php, redirect him to without .php extension Page.
This is my htaccess but it does not redirect user to without extension page.
RewriteCond %{REQUEST_URI} ^Show\.php$
RewriteRule ^Show\.php$ ./Show [R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^Show$ ./Show.php [L]
REQUEST_URI, unlike the RewriteRule expression, begins with a leading / but your expression begins with ^Show. Simply adding the leading slash should do the job. Everything else looks correct.
RewriteCond %{REQUEST_URI} ^/Show\.php$
#-------------------------^^^^^
RewriteRule ^Show\.php$ Show [R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#Rewrite to Show.php rather than ./Show.php
RewriteRule ^Show$ Show.php [L]

htaccess server side rewrite not working

I have my htacess rewrite working, the pages are going to where they are supposed to, but the url bar changes and I dont want it to. I thought this was an INTERNAL redirect and whatever is in the URL would be displayed. It's not working that way.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/[^/]+(/(.+))?$
RewriteRule . /%2 [L]
Basically, the url IS rewriting to the new URL. How do I get it to not do that?
Per your last comment, try this instead
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/[^/]+(/?|/(.+))$
RewriteRule . /%2 [L]
It should match an optional trailing slash e.g. http://mydomain.com/somedir or http://mydomain.com/somedir/ and one with a directory after e.g http://mydomain.com/somedir/dir2
If whatever the %2 back reference is matching doesn't end with an extension, mod_dir might think that it's a directory. If it's missing a trailing slash, mod_dir will externally redirect the browser to the same URL but with a trailing slash. You could try turning DirectorySlash Off in your .htaccess file or server config.
edit
You can try to bypass mod_dir's by doing the directory check yourself and adding the trailing slash so mod_dir won't redirect you. It would look something like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/[^/]+(/(.+))$
RewriteCond %{DOCUMENT_ROOT}/%2 -d
RewriteRule ^[^/]+(/(.+))$ /$2/ [L]

apache mod-rewrite

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([0-9a-zA-Z_\-]+)(|\.html|\.php)$ page.php
this is the rule is .htacess.
what I want is if file is not found then redirect to page.php.
for example if url people.php ,people.html is not found then redirect to page.php.
because I also want those filenames without .php like people direct to people.php first. then check file exist or not, if not redirect to page.php.
how do I add one rule to make people redirect to people.php
These rules should work for you:
RewriteEngine on
Options +FollowSymlinks -MultiViews
# if there is no . in request then append .php
# it is important to make [L] as last rule here
RewriteCond %{REQUEST_FILENAME} !\.
RewriteRule ^(.+)$ /$1.php [L]
# 404 handling for files that don't exist
# NC is for ignore case comparison
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^.]+\.(html|php)$ /page.php [L,NC]
See if this does what you're looking for:
RewriteEngine on
RewriteRule ^(?:.+/)?([^/.]+)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule \.(?:html|php)$ page.php

Resources