.htaccess rewrite optional trailing slash - regex

I currently have my website excluding the .html using the following code in .htaccess:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
And this works great, but if I add a trailing slash I get a 404.
I'd like to have the option of having it work as it is now and also if I add a trailing slash at the end.
Is this possible?
Thanks

Sure you can use:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^(.+?)/?$ $1.html [L]

Related

RewriteRule does not work with trailing slash in .htaccess

I have a RewriteRule in my .htaccess that works almost as I wanted.
What I want is a rule that works with an optional trailing slash.
foo.com/bar > foo.com/index.php?p=bar
foo.com/bar/ > foo.com/index.php?p=bar
What I have is this;
RewriteEngine On
RewriteCond %{REQUEST_URI} !^(\.png|\.jpg|\.gif|\.jpeg|\.bmp|\.pdf)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/?$ index\.php\?p=$1 [NC,L]
But this doesn't seem to work when the trailing slash is added.
You may use this rule to allow an optional trailing slash:
RewriteCond %{REQUEST_URI} !\.(png|jpe?g|gif|bmp|pdf)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?p=$1 [QSA,L]

mod rewrite condition fix to avoid partial word match

Its probably very simple, but I can't make it work =/
I have this rule:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^admin/?(.+?)?$ adm.php?route=$1&%{QUERY_STRING} [L]
Well, its suppose to send the user to the adm.php when there is a localhost/admin or localhost/admin/anything url, preserving the query string.
It works as expected, except for the fact it's matching any word that starts with 'admin', for example:
localhost/admin/news/list?page=1 => adm.php?route=news/list&page=1
localhost/adminbool/news/list?page=1 => adm.php?route=bool/news/list&page=1
Both of rewrite works, but that extra 'bool' messes everything in my routing process.
How can I make it respond only to a 'admin' exact match?
Any ideas?
Thanks!
EDIT
I guess im not clear enought.
Here is the full .htaccess and a brief comment about how its suppose to work
Options -Indexes
Options -MultiViews
<FilesMatch "\.(tpl|ini|log|txt)">
Order deny,allow
Deny from all
</FilesMatch>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(adm/|app/|lib/|sys/) - [F,L,NC]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!admin/?$)(.+?)/?$ index.php?route=$1&%{QUERY_STRING} [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^admin(/.*)?$ adm.php?route=$1 [L,QSA]
</IfModule>
the index.php file refers to /app (front-end)
the adm.php refers to the /adm (back-end)
the urls of the front-end has no prefix, while the back-end urls has the /admin/ prefix
example:
localhost/admin - back-end
localhost/admin/ - back-end
localhost/admin/news/list?order=date - back-end
localhost/ - front-end
localhost/admine - front-end
localhost/administration-whatever.html - front-end
localhost/admine123-anything.html - front-end
Couple of issues:
Your regex is incorrect
You're not using helpful QSA flag.
Use this rule with correct regex and QSA:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^admin(/.*)?$ adm.php?route=$1 [L,QSA]
QSA (Query String Append) flag preserves existing query parameters while adding a new one.
Your rewrite rules:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(adm/|app/|lib/|sys/) - [F,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^admin(/.*)?$ adm.php?route=$1 [L,QSA]
RewriteCond %{REQUEST_URI} !/adm\.php [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!admin/?$)(.+?)/?$ index.php?route=$1 [L,QSA,NC]
Try this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^admin[$|/*](.+?)?$ adm.php?route=$1&%{QUERY_STRING} [L]

mod_rewrite does not redirect to my internal url

i'm trying to solve a mod_rewrite rule for my webshop.
The URL is: localhost/myshop/category1/category2/myproduct.html
The rediret URLshould be: localhost/myshop/configurator/product/configure/id/1/s/myproduct/category1/5/
If I try a RewriteRule like this
RewriteRule ^myproduct\.html$ http://localhost/myshop/configurator/product/configure/id/1/s/myproduct/category1/5/
nothing happens and the redirect will not appear.
What i'm doing wrong?
my rewrite rules in .htaccess
RewriteBase /myshop/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
RewriteRule ^myproduct\.html$ http://localhost/myshop/configurator/product/configure/id/1/s/myproduct/category1/5/
I want that myproduct.html redirects immediatly to the configurator link, so i want to implement a RewriteRule
Try re-ordering your rules:
RewriteEngine On
RewriteBase /myshop/
RewriteRule (^|/)myproduct\.html$ /myshop/configurator/product/configure/id/1/s/myproduct/category1/5/ [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]

rewrite rule for WordPress 3.3 permalinks is not working

Every since an upgrade to WordPress 3.3 URLs are not redirecting as they should.
Changed: domain.com/2010/10/postname/ to: domain.com/postname/
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/[0-9]{4}/[0-9]{2}/(.+)$ /$1 [NC,R=301,L]
The problem was due to the leading slash and not using $3
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]{4})/([0-9]{1,2})/(.+)$ /$3 [NC,R=301,L]
There's a script here you can use to generate .htaccess rules if you want to change permalinks to the /%postname%/ structure.
http://yoast.com/change-wordpress-permalink-structure/
My permalinks were exactly the same as yours, I used this tool to change them and it is working well.
The last rule will never get applied if the previous rule matches. Assuming that the http://domain.com/2010/10/postname/ request doesn't match a file or directory, the RewriteRule . /index.php [L] is going to rewrite the URI to /index.php thus it'll never get to your rule. Try moving your rule up to the top, just below RewriteBase /, and duplicate the !-f/!-d conditions, so that it looks like this:
RewriteBase /
# for 301 redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/[0-9]{4}/[0-9]{2}/(.+)$ /$1 [NC,R=301,L]
# the rest of the rules
RewriteRule ^atom.xml$ feed/ [NC,R=301,L]
RewriteRule ^rss.xml$ feed/ [NC,R=301,L]
RewriteRule ^rss2.xml$ feed/ [NC,R=301,L]
RewriteCond %{HTTP_USER_AGENT} !FeedBurner [NC]
RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC]
RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feeds.feedburner.com/handle [R=302,NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Also, if this is in an .htaccess file, you need to remove the leading slash in the rule match so that it looks like this: ^[0-9]{4}/[0-9]{2}/(.+)$

ajax file not loading because of trailing slash

I have an ajax file that is called when someone begins to type in search bar. I have recently been cleaning up my urls and removing file extentions adding trailing slashes, since then my ajax file doesnt appear to load anymore. can anyone help? here my htaccess so far
Options +FollowSymlinks
Options +Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
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]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^([^.]+)\.php$ /$1 [R=301,L]
Assuming that your AJAX requests go to the /includes folder, and your normal pages do not, we can modify your rules a bit so that they look like this (including Cags' comment about the RewriteCond):
Options +FollowSymlinks
Options +Indexes
RewriteEngine on
# We'll do the redirect first, so no other rules get in the way
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.#?\ ]+)\.php([#?][^\ ]*)?\ HTTP/
# Make sure the request didn't start with "includes"
RewriteCond %1 !^includes/
RewriteRule ^([^.]+)\.php$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d
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 also think that you either wanted /? at the end of the rules in the center block, or /$1/ as the replacement on the rule in the first block, so that the redirect from /page.php to /page gets interpreted correctly after the first redirect (I think right now you'll get redirected twice, once by the first block, and again by the last block).