1

I'm trying to figure out how I can strip the file extension from the url. I want to change www.website.com/index.php to show www.website.com/index.

I have been trying to get this to work

RewriteRule ^about$ about.php [L]

When I plug this code in to an htaccess file all it does is distort my website, actually it looks like it removes the css, weird..

I'm running my website locally on a wamp server.

Any advice on how I can get this to work?

  • 1
    Duplicate of [Hide Page Extensions (Like StackOverflow)](http://stackoverflow.com/q/1759420/209139) or of [How can I use .htaccess to hide .php URL extensions?](http://stackoverflow.com/q/10028025/209139). – TRiG Mar 25 '13 at 11:06
  • I tried that code, and I'm not getting the errors anymore, but the extensions are still there. –  Mar 25 '13 at 11:16
  • 1
    Those `.htaccess` scripts will make pages work both *with* and *without* file extensions. They won't redirect pages with extensions to ones without. Change your links to not include file extensions, and everything should work. – TRiG Mar 25 '13 at 11:24
  • Any advice on changing requests like this (website.com/profile?name=christian) to (website.com/profile/christian). –  Mar 25 '13 at 11:44
  • 1
    That's been asked here before somewhere too. Try searching for htaccess rewrite. – TRiG Mar 25 '13 at 12:11

2 Answers2

2

This most likely a result of you use relative paths for linking your stylesheets. Example:

<link rel="stylesheet" type="text/css" href="css/style.css" />

When you visit http://example.com/about/, the web browser is looking for the stylesheet in: http://example.com/about/css/style.css.

To fix this, use absolute paths, which start from your web root:

<link rel="stylesheet" type="text/css" href="/css/style.css" />

Note: you will probably have to use absolute paths for your images as well.

  • 1
    It's not property etiquette to entirely change your question after answers have been posted, however, the internal server error issue may be caused by Apache not having mod_rewrite enabled. Can you verify that it is? –  Mar 25 '13 at 11:11
  • The style was working fine, then I got a server error.. changing the question was removing a problem I didn't have anymore. I will check to see if it is. –  Mar 25 '13 at 11:19
  • I changed #LoadModule rewrite_module modules/mod_rewrite.so to LoadModule rewrite_module modules/mod_rewrite.so and reset wamp. Still nothing :/ –  Mar 25 '13 at 11:22
  • Got it working, my bad. I was still on index.php, and when I removed the .php it worked.. –  Mar 25 '13 at 11:24
  • Is there anyway to auto direct to index from index.php, just in case they specified it. –  Mar 25 '13 at 11:26
  • @ChristianRankin: Perhaps something along the lines of: `RewriteRule ^(.*)\.php$ $1` –  Mar 25 '13 at 11:32
  • 1
    @ChristianRankin. If there's a problem you don't have any more, **don't** remove it from the question. Instead, mark the question as answered. Stack Overflow is not your personal debugging service: we expect questions and answers to provide value to other people too. Don't remove them when *you* don't need them any more. – TRiG Mar 26 '13 at 11:05
0
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

try adding these lines to .htacess in root of your site

alwaysLearn
  • 6,882
  • 7
  • 39
  • 67