justlukeyou Posted May 19, 2013 Share Posted May 19, 2013 Hi, I am trying to remove the .php from the end of pages. The problem I have tried around ten different options and none of them work, but how come there are so many different options? http://stackoverflow.com/questions/9821222/remove-php-extension-explicitly-written-for-friendly-url http://css-tricks.com/snippets/htaccess/remove-file-extention-from-urls/ RewriteEngine On RewriteCond %{SCRIPT_FILENAME} !-d RewriteRule ^([^\.]+)$ $1.php [NC,L] Quote Link to comment Share on other sites More sharing options...
Eiseth Posted May 19, 2013 Share Posted May 19, 2013 Are you sure you save it as .htaccess? If you're working on localhost, make sure your apache is rewritable I have similar problem before http://phpcollection.com/mod-rewrite-windows-wamp-xampp/ - windows http://blog.valugi.ro/2008/05/29/htaccess_internal_server_error_apache2_ubuntu/ - ubuntu Or if you want an easy solution, try putting each pages into folder and rename the files to index.php For example the about.php page will be about/index.php, so whenever you link to about page it will load the page without the "index.php" Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 19, 2013 Share Posted May 19, 2013 \ Or if you want an easy solution, try putting each pages into folder and rename the files to index.php That is in fact about the farthest you could get from an easy solution. Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 19, 2013 Share Posted May 19, 2013 how come there are so many different options? Since this is the only question you actually asked in your post, I'm going to go with: Because. Computers. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted May 19, 2013 Author Share Posted May 19, 2013 Hi, Is there a standard method of making a link work wthout .php at the end? I have tried around 15 different options but none of them work. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted May 19, 2013 Share Posted May 19, 2013 It might be helpful if you told everyone what version of Apache you're using and what (with examples) you've tried so far. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted May 19, 2013 Author Share Posted May 19, 2013 Thanks, This is what Im currently using. How can I get the apache version? RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)/$ $1.php RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php RewriteCond %{REQUEST_FILENAME}.html -f RewriteRule ^(.*)/$ $1.html RewriteCond %{REQUEST_FILENAME}.py -f RewriteRule ^(.*)/$ $1.py Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted May 19, 2013 Author Share Posted May 19, 2013 Hi, Does anyone have any suggestions please on how I can fix this. It appears simple but what ever I try doesn't seem to work. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted May 19, 2013 Author Share Posted May 19, 2013 Hi, Its Apache 2.2 Quote Link to comment Share on other sites More sharing options...
sford999 Posted May 20, 2013 Share Posted May 20, 2013 You can use the code below which will give you URLs like this: ht tp://www.domain.com/somepage RewriteEngine On RewriteBase / RewriteRule ^index$ index.php [QSA,L] RewriteRule ^contact$ contact.php [QSA,L] RewriteRule ^search$ search.php [QSA,L] RewriteRule ^privacy-policy$ privacy.php [QSA,L] RewriteRule ^terms-of-service$ terms.php [QSA,L] Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted May 20, 2013 Share Posted May 20, 2013 @justlukeyou, you can apply multiple rules to the same document, but....it's not allowed to redirect the request to the same page! Solution: Aplly fake urls and redirect them to a different (real) directory on the same project. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted May 20, 2013 Author Share Posted May 20, 2013 You can use the code below which will give you URLs like this: ht tp://www.domain.com/somepage RewriteEngine On RewriteBase / RewriteRule ^index1$ index1.php [QSA,L] RewriteRule ^contact$ contact.php [QSA,L] RewriteRule ^search$ search.php [QSA,L] RewriteRule ^privacy-policy$ privacy.php [QSA,L] RewriteRule ^terms-of-service$ terms.php [QSA,L] Hi, I tried this but it doesn't work. Jazzman1, Lets say I have a link like www.website.com/index1.php I am very confused by what you by "Aplly fake urls and redirect them to a different (real) directory on the same project." I thought their would be a standard way to do this? Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted May 20, 2013 Share Posted May 20, 2013 (edited) Alright then, let's doing the test. Assuming the we have the next structure: / # web root directory /project # the folder of your project # in that exampe let's say you have two .php files and one .htaccess /project/test.php #contains <?php echo "project testing file" ?> /project/debug.php #contains <?php echo "project debugging file" ?> /project/.htaccess The content of the .htaccess file should be: RewriteEngine on RewriteCond %{REQUEST_URI} ^/project/([a-z]+)\.php$ RewriteRule ^(.*)$ /project/%1 [R=301,L] RewriteCond %{SCRIPT_FILENAME} !-f RewriteRule ^(.*)$ /project/$1.php [L,QSA] Now if someone try to achieve for instance /project/test.php you will see that the URL is going to change from /project/test.php to /project/test, but also you will get a message: The page isn't redirecting properly b/s you're trying to redirect the same request to the same page.But now let's make another test.Delete only the php files inside the project folder and create a new sub-directory for this test we will named it - /project/webInside this new web directory create two files test.php and debug.php. And change the rule of .htaccess: RewriteEngine on RewriteCond %{REQUEST_URI} ^/project/([a-z]+)\.php$ RewriteRule ^(.*)$ /project/%1 [R=301,L] RewriteCond %{SCRIPT_FILENAME} !-f RewriteRule ^(.*)$ /project/web/$1.php [L,QSA] Now that files are going to be reached as URL's - /project/debug.php or /project/debug even they don't exist inside /project folder. If some smart user try to achieve for instance /project/web/debug.php, you could create a new .htaccess file in this folder with a new rule of a forbidden flag (forbidden|F ) telling Apache not to provide a page in response to this request. Edited May 20, 2013 by jazzman1 Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted May 20, 2013 Author Share Posted May 20, 2013 Hi, I have a director called "siteinfo" so I tried the following but it had no affect. Can I just check two things: 1. Does the file need to save in a certain format. I am just using Notepad and saving it in a test file 2. Is the file called "htaccess" or ".htaccess" I have tried both and tried to save the file in different formats but still no impact. RewriteEngine on RewriteCond %{REQUEST_URI} ^/siteinfo/([a-z]+)\.php$ RewriteRule ^(.*)$ /siteinfo/%1 [R=301,L] RewriteCond %{SCRIPT_FILENAME} !-f RewriteRule ^(.*)$ /siteinfo/$1.php [L,QSA] Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted May 20, 2013 Share Posted May 20, 2013 (edited) Check Apache conf whether a mod_rewrite module is installed and enabled too. May I ask you something? Is there some reasonably reason to do this? I just provide you en axample, but not sure how mutch effectively an flexible is that if you have a complex web structure. Edited May 20, 2013 by jazzman1 Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted May 20, 2013 Author Share Posted May 20, 2013 Hi, Raised a case with my host and they advised that their is nothing stopping a mod_rewrite from working Im looking pretty URLs so they are appear better. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted May 20, 2013 Share Posted May 20, 2013 Looking pretty for visitors but not for the server (machine) The test I made it in my local CentOS server and everything works just fine. Make sure that a tree directory structure is correct. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted May 20, 2013 Author Share Posted May 20, 2013 Hi, Im quite confused though. I though their would be a standard method to aichieve this? Is itpossible that Im doing something wrong like saving the file in the wrong format? Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted May 20, 2013 Share Posted May 20, 2013 (edited) Did you try to run the test onto a local machine? Also, make sure that the cache of your browser is disabled or delete cookies, etc.... Edited May 20, 2013 by jazzman1 Quote Link to comment Share on other sites More sharing options...
DaveyK Posted May 21, 2013 Share Posted May 21, 2013 (edited) <IfModule mod_rewrite.c> Options +FollowSymlinks -MultiViews RewriteEngine On </IfModule> RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.php [NC,L] This doesnt work? For me, using XAMPP, this enables: localhost/index localhost/user Where index.php and user.php are files in my folder. Edited May 21, 2013 by DaveyK Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted May 21, 2013 Share Posted May 21, 2013 @DaveyK, there is no problem to use urls like /localhost/index poiting to /localhost/index.php. He wants, when the browser send a reques for instance /localhost/index.php the index.php not to be served from Apache. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.