-2

Ok, So iv'e decided to remove all .php extentions from the url, and for this iv'e used the simple method:

Options MultiViews+

However, My profile page now displays almost nothing, with a black background because of the URL. If I input:

profile.php?username=Joe.Bills

It works perfectly fine, If however, I use:

profile?username=Joe.Bills 

It automatic changes the url and adds a / AFTER profile Example:

profile/?username=Joe.Bills

And thus, thinking its in a "separate directory" causes the error. How can i remove it from adding the additional / so it doesn't think its going to a separate directory

P.S. My link's are correct and currently is this:

<a href="profile?username=<?php echo $_SESSION['user_name']; ?>"><span class="bg">Profile</span></a>

Again, as I said, If I put

profile.php?username=Joe.Bills 

It works completely fine, so I don't understand why the URL thinks its a directory when entered, So my question would be:

How can I remove .PHP extensions a different way to fix this, or is there a way I can fix this with what I currently have and how?

Joe Bills
  • 31
  • 5
  • Check http://stackoverflow.com/questions/8371634/how-to-hide-php-extension-in-htaccess –  May 04 '16 at 17:39
  • You can't just remove the .php extension, you need to do some research on url rewrite. Depending on whether you are using apache or nginx you will need to figure out how to do rewrites for that individual web server. – Pitchinnate May 04 '16 at 17:40
  • I did use this too: RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L, QSA] – Joe Bills May 04 '16 at 17:44
  • But it gave me an internal error? – Joe Bills May 04 '16 at 17:45

1 Answers1

2

You can use the following instead of Options +Multiviews :

 RewriteEngine on
 #remove trailing slashes
 RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !/$ %{REQUEST_URI}.php [NC,L]

This will allow you to access .php files without extension/slash at the end.

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • i have tried this already which did give me an internal error, though i enabled rewrite_mod on my server, and it removed the .php, however when i go to profile, it still changes it with the `/` so this has something to do with my request maybe for the user? but I have no clue why this is happening. I have no links that have a `/` in so im unsure of where the error is coming from – Joe Bills May 04 '16 at 18:03
  • @Joebills any other rules in the htaccess? – Amit Verma May 04 '16 at 18:05
  • Okay, So i changed the request " $name = $_REQUEST['username']; " to "$name = $_REQUEST['user'];" which then displayed as " profile?user=Joe.Bills " which worked fine, but i want it to be "username" not "user". Is there a way I could change it without the `/` happening again? – Joe Bills May 04 '16 at 18:07
  • @JoeBills I am not good at php , not sure why the slash is appearing. Could you post your full htaccess in the question? – Amit Verma May 04 '16 at 18:16
  • the only rule in my .htaccess is what you have posted in your answer. – Joe Bills May 04 '16 at 18:18
  • @Joebill then the rule should work. Please check your php if you are still having the issue – Amit Verma May 04 '16 at 18:21
  • Sorry I don't think you understand what I meant. Okay, So the rule works fine, it displays all pages without the .php extention. However, when I go to `http://localhost/profile?username=Joe.Bills` it changes it to `http://localhost/profile/?username=Joe.Bills` (adding a `/` after `profile`) BUT if I change the PHP Request code on my profile page, to "user", it would then be `http://localhost/profile?user=Joe.Bills` THEN it works fine. It just doesn't work if the request is set to "username" I'm not sure why – Joe Bills May 04 '16 at 18:28
  • @Joebills I have adde a new rule to remove trailing slashes. – Amit Verma May 04 '16 at 18:41
  • 1
    Ok that worked perfectly, Thank you for your help! I have no idea what or how .htaccess worked, but thank you! – Joe Bills May 04 '16 at 19:52