-1

I've got a url http://example.com/pages.php/page-name and I want to remove the .php extension on the pages.php part... Is that possible with an .htaccess file or should I just give up?

I've tried RewriteRule ^(.+)\.php/(.+)$ $1/$2 from "Remove php extension from url" with no luck.

I'm new to .htaccess files (this is the first site my normal tricks don't work)

Community
  • 1
  • 1
jay013
  • 1
  • It's very possible, a rewrite rule would do that – adeneo May 07 '15 at 14:36
  • Are the files actually in a directory/folder names pages.php? If so, is there a reason you can't rename it to pages? – kainaw May 07 '15 at 14:37
  • They're not actually in a directory. It's just pulling the page's content from the database and using pages.php to render it. Usually I can just use a "pages" file without the extension to render the pages, but this server isn't letting the links work properly. – jay013 May 07 '15 at 14:44

1 Answers1

0

You can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+([^/.]+)\.php/(\S+)\s [NC]
RewriteRule ^ %1/%2? [R=302,L,NE]

# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/([^/]+)/?$ $1.php/$2 [L,NC]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thanks, that kind of worked... but the css & js files weren't loading anymore. Still looking for a solution. – jay013 May 07 '15 at 14:50
  • css/js isn't working due to your use of relative paths. To fix you can add this in the `` section of your page's HTML: `` so that every **relative URL** is resolved from that base URL and not from the current page's URL. – anubhava May 07 '15 at 14:54
  • All the paths are already resolved from the baseURL using slashes. – jay013 May 07 '15 at 15:17
  • Thanks again, that almost worked. It worked to take off the .php extension but now it only goes to the home page. The content from the sub pages isn't being rendered. – jay013 May 07 '15 at 16:23
  • Looks like you are adding new problems now. You didn't ask for supporting subpages initially. I suggest posting a new question for sub page support and marking this as accepted. – anubhava May 07 '15 at 16:44