Reputation: 5622
I have a .htaccess placed in server.com/site_admin/api (actual directory structure: /public_html/site_admin/api)
I want to direct all requests to the corresponding php file. So api/users would rewrite to api/users.php
So I wrote this rule:
RewriteEngine On
RewriteRule ^(.*)$ $1.php
But it doesn't work. I get a 404 when I access the url server.com/site_admin/users
When I added a [R=301] to it
RewriteRule ^(.*)$ $1.php [R=301]
I found that it redirects to http://server.com/home/server_user/public_html/site_admin/api/users.php Instead of http://server.com/site_admin/api/users.php
Can someone please help me out here
Upvotes: 2
Views: 2334
Reputation: 11890
Use this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !php$
RewriteRule ^(.*)$ $1.php
Yours doesn't work because the rewrited url is still matched by (.*)
, so it will rewrite it again until cause an error. But it's strange it gives you a 404 instead of 500...
The second one doesn't work because [R]
generate a new GET request, so you need to add the host or it will request the absolute path to the defined host and it, obviously, will not work. If you want to use [R], you should use:
RewriteRule ^(.*)$ http://server.com/site_admin/api/$1.php [R]
But it will not solve your problem. As I said, it will generate a new GET request and .php
will be visible to your user.
Upvotes: 2