How to rewrite any url that doesn't match a specific pattern - .htaccess

I want to rewrite any url that doesn't match the pattern *.php
Example: domain.com/abc.html or domain.com/abc should be rewritten to domain.com/abc.php

For rewriting .html extensions to existing .php files with the same name, try this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ $1.php?%{QUERY_STRING} [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php [L]

Just a quick solution:
if len(url.split('.php'))==1:
extention = '.' + (url.split('.')[-1])
url = url.replace(extention,'.php')
But not a good practice to do this way

Related

Parameter exception in htaccess rewrite

I'm trying to achieve an exception in my htaccess rewrite, so that all requests go to page=[request] except for parameter "lang" so that the rewrites would be like:
www.url.com > www.url.com?page=
www.url.com/pagename > www.url.com?page=pagename
www.url.com?lang=en > www.url.com?page=&lang=en
My htaccess is now:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ ?page=$1 [QSA,L]
And the rewrite works nicely on all subpages but not in root/index.
Due to presence of [^/]+ in your pattern it won't match landing page.
You should use this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* ?page=$0 [QSA,L]
.* will match everything including landing page.

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 prevent changes rewritten url

I have following .htaccess rewirite rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (/m)?/([^/]+)\.(jpg|jpeg|gif|png)$
RewriteRule .* http://domain.com/user/upload%1/%2.%3
So, now link transforms (like redirect) from http://domain.com/12345.png to http://domain.com/user/upload/12345.png.
What have I to fix (I think, it should be some flag), if I want to prevent this transformation?
Remove the http://domain.com from the target of your rule:
RewriteRule .* /user/upload%1/%2.%3 [L]

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

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]

.htaccess How to check if URI + file extension doesn't exists before RewriteRule

When a page is accessed via an URL like this:
/pagename
How can we check if the following file exists via htaccess
/pagename.htm
And then load index.php if not?
Here's what I'm working with:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
I've tried a variation of this but no luck:
RewriteCond %{REQUEST_URI} !/.htm -f
# If the URI is not .htm and doesn't have any additonal params, try .htm
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?![^/]+\.htm)([^/]+)$ $1.htm [L,R=302]
# Adding the R flag causes the URI to be rewritten in the browser rather than just internally
# If the URI is .htm but it doesn't exist, pass to index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/\.]+\.htm)$ $1/no [L]
# Passes requested path as 'q' to index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/no/?$ index.php?q=$1 [L]
Should work rather elegantly for you. If it can't find the file, it just passes it on to index.php. You can of course modify what variable it uses, I just used 'q' for generic purposes.
Also, do note that you can condense this down to a single set of RewriteConds. I've expanded them for annotation purposes here.

Resources