.htaccess Rules Removing .php while forwarding real 404s [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
remove .php extension with .htaccess
I know little about .htaccess, What I'm trying to accomplish is two-fold.
Allow requests to .php to be called with or without the .php and
with or without a trailing slash. For example.
/profile.php works
/profile appends .php, works
/profile/ append .php so it works
If the file or directory doesn't exist after trying the above, then
treat as a 404 and redirect to another page.
/something failure, redirect to /404.php?id=$1 (url requested)
I have two rules that work separately, but I need to conditionalize these or something. Any advice would be appreciated.
RewriteEngine on
# This works for the PHP part.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/?$ $1.php
# This works for general 404 handling
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /404.php?url=http://%{HTTP_HOST}/$1 [L]

Apache will automatically append .php to /profile and /profile/ without any mod_rewrite. The only thing you should handle is the 404 page.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /404.php?url=http://%{HTTP_HOST}/$1 [L]
However, if it's not working for some reason (e.g. apache wasn't configured well):
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /404.php?url=http://%{HTTP_HOST}/$1 [L]

Related

.htaccess rewrite specific URL segment to a PHP file and then remove the php file from the URL?

I have a wordpress site and a custom build app all on the same URL.
I am looking to redirect all my CMS (custome app) URLs to a certain php controller file. Once redirected I am looking to remove the php file from the URL
e.g. www.website.com/cms redirects to www.website.com/backend.php/cms but the URL continues to display as www.website.com/cms
So far I have the following:
# Handle the custom App URLs
RewriteCond %{HTTP_HOST} ^(www.)?website.com/cms$
RewriteCond %{REQUEST_URI} !^backend.php/cms/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ backend.php/cms/$1
# Handle the Wordpress URLs by removing index.php
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
EDIT:
The problem is what I have does not work.
If I go to www.website.com/cms I get a 404.
If I go to www.website.com/backend.php/cms the URL does not change to www.website.com/cms, it stays as www.website.com/backend.php/cms
I am not great at .htaccess but think I am going down the correct route.
Did you try just routing by url:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/cms/(.*)$ /backend.php [L]
#or RewriteRule ^/cms/(.*)$ /yourcms/backend.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
All your magic about 'understanding' the url you do in your cms reading REQUEST_URI.
I ended up fixing this myself by having the following RewriteRule
RewriteRule ^/?cms([A-Za-z0-9\-_\/]+)/?$ /backend.php [L]
RewriteRule ^/?cms /backend.php [L]
Thanks to those that tried to help :)

Request param from url in mvc using htaccess and php

The real url format is
http://localhost/myfolder/index.php?url=login/register&step=1
After mod rewrite I get
http://localhost/myfolder/login/register/step/1
All good but how can I get the value of step in a php file.If I do $_GET['step'] then I get nothing.
My htaccess
Options -MultiViews
# turn rewriting on
RewriteEngine On
# When using the script within a sub-folder, put this path here, like /mysubfolder/
# If your app is in the root of your web folder, then please delete this line or comment it out
RewriteBase /mybb/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)/(.*) index.php?url=$0&step=$2 [L,QSA]
Change your rule to:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)(?:/(step)/([0-9]+))?/?$ index.php?url=$1&$2=$3 [NC,L,QSA]

Two .htaccess rules not working with each other

I have the following .htaccess:
RewriteEngine On
RewriteBase /shared/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /shared.php [L]
But I would also like to remove .php extension with the following:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
I have tried a dozen combinations based on answers elsewhere not just on StackOverflow but still cannot get it right, I either render pages not in shared directory unable to open with 500/404 errors or 500 error when I go to /shared.
After further investigation and trial when I add the rules to remove the .php extension it messes up the first rule to route anything under the path of /shared/ to shared.php the path /shared/username are not real locations but the script insures that the correct information is presented. It would be handy to ignore the second rule if the URL has /shared/ in the path? Is that possible? I am not rewriting everything to the /shared/ directory - only when the path reads /shared/username do I want that rule to kick in, everything else should be rewritten to the / base directory.
Keep your rules like this:
RewriteEngine On
RewriteBase /shared/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /shared.php [L]

htaccess-rewriting, but excluding sub-directory [duplicate]

This question already has an answer here:
.htaccess exclude a directory to be rewrited
(1 answer)
Closed 9 years ago.
I have a general rewrite-rule in my sites root which looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w]+)/?([\w]+)? index.php?action=$1
which works fine for rewriting URLs like "www.example.com/myAction" to "www.example.com/index.php?action=myAction"
This must stay like this, but I need to exclude a subdirectory ("/login") from rewriting.
So I tried:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/login/
RewriteRule ^([\w]+)/?([\w]+)? index.php?action=$1
but still no luck. I've read several answers here on SO but can't find the flaw. Any ideas? Thank you!
You've an extra slash. .htaccess doesn't use the preceding slash unlike a Virtual Host. Also /login/ is a directory. So you're matching anything that is not a real file, but could potentially be a real directory. So if for instance '/css/' exists, you're rewriting it to index.php as an action on request.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^login/
RewriteRule ^([^/]+) index.php?action=$1
If it's still acting unexpectedly you might need to make sure you're not rewriting existing directories and try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^login/
RewriteRule ^([^/]+) index.php?action=$1
I also noticed that you're only capturing up to the first slash (possibly) with your posted example code since you're using the ? look-up, so in my previous layout's I'm showing that code a little shorter. To capture the whole URI you would use this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^login/
RewriteRule ^(.*) index.php?action=$1
To capture a second (and third) string you can do something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^login/
RewriteRule ^([^/]+)/([^/]+)/([^/]+) index.php?action=$1&page=$2&response=$3
What you have should work, but you can alternatively try to have all requests for /login/ be passed through instead:
RewriteEngine On
RewriteRule ^login/? - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w]+)/?([\w]+)? index.php?action=$1

TinyMCE conflicting with htaccess

I have configured my server using htaccess so that I can route all URL's into a PHP $_GET parameter. I want the URLs on the site to finish with .htm. Currently I have this htaccess, and I have never had any problems with it:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ajax/([A-Z,a-z,0-9,\_/]*).htm$ index.php?route=$1&opformat=ajax [QSA]
RewriteRule ^([A-Z,a-z,0-9,\_/]*).htm$ index.php?route=$1 [QSA]
However I have just installed TinyMCE which uses .htm files in the js folder e.g. "/js/tinymce/prop.htm". For some reason when this URL is entered my htaccess routes it to php and it doesn't work.
I was given to understand that RewriteCond %{REQUEST_FILENAME} !-f should stop this from happening. Why is it not? RewriteCond %{REQUEST_FILENAME} !-d is working as I can go to "/js/tinymce" in this example and see directory listing.
Thanks.
I suggest you to change your code to this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^ajax/([^.]*)\.htm$ index.php?route=$1&opformat=ajax [L,QSA,NC]
RewriteRule ^([^.]*)\.htm$ index.php?route=$1 [L,QSA,NC]

Categories