0

I am trying to add some other page for my project. But i have one question about php extension.

So i am using htaccess for some url like following htaccess:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]

RewriteRule ^friends/([\w-]+)/?$ /friends.php?username=$1 [L,QSA]
RewriteRule ^followers/([\w-]+)/?$ /followers.php?username=$1 [L,QSA]
RewriteRule ^status/([\w-]+)/?$ /status.php?msgID=$1 [L,QSA]
RewriteRule ^following/([\w-]+)/?$ /following.php?username=$1 [L,QSA]
RewriteRule ^followers/([\w-]+)/?$ /followers.php?username=$1 [L,QSA]
RewriteRule ^(.*/([a-zA-Z0-9_-]+)|([a-zA-Z0-9_-]+))$ /profile.php?username=$1 [L,QSA]

Now, i have created photos.php from the details folder and the link is :

<a href="http://www.name.com/details/photos.php">Go Photos</a>

When i click the Go Photos link i want to remove that url php extension like

http://www.name.com/details/photos

I have still tryed some answer from stackOverflow

Remove .php extension from url

Remove php extension from url

but that answers gives me 404 page not found.

How can i remove .php extension from url anyone can help me here ?

Community
  • 1
  • 1
AlwaysStudent
  • 1,354
  • 18
  • 49
  • Are you asking how to use javascript to remove an extension from a hyperlink before its clicked? – omghai_8782 Jun 24 '16 at 18:40
  • Possible duplicate of [Remove .php extension (explicitly written) for friendly URL](http://stackoverflow.com/questions/9821222/remove-php-extension-explicitly-written-for-friendly-url) – J.J. Hakala Jun 24 '16 at 21:36

1 Answers1

3

You need additional rules for removing .php extension.

Use these rules:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]

RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index\.php$ /$1 [L,R=302,NC,NE]

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^status/([\w-]+)/?$ status.php?msgID=$1 [L,QSA]

RewriteRule ^(following|followers|friends)/([\w-]+)/?$ $1.php?username=$2 [L,QSA]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteRule (?:^|/)([\w-]+)/?$ profile.php?username=$1 [L,QSA]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Dear i have used also some other think like : `` the explore is explore.php if i use your codes then i can not open explore page it says Internal Server Error. How can i fix this problem ? – AlwaysStudent Jun 24 '16 at 18:51
  • for example https://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/ – AlwaysStudent Jun 24 '16 at 18:56
  • oww also following,followers and friends links given 500 Internal Server Error – AlwaysStudent Jun 24 '16 at 18:58
  • @anumbhava it is localhost:8888 from my imac . I don't know how can i check error.log – AlwaysStudent Jun 24 '16 at 19:00
  • page looks like this : [IMAGE](https://scontent-frt3-1.xx.fbcdn.net/t31.0-8/13495499_304514426548390_8716549526317603430_o.jpg) – AlwaysStudent Jun 24 '16 at 19:02
  • i found it : `[Fri Jun 24 21:57:35 2016] [error] [client ::1] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: http://localhost:8888/mustafa` – AlwaysStudent Jun 24 '16 at 19:05
  • Now the rules working fine Thank you. Just one problem here this link gives me same error 500 Internal Server Error : . I don't have explore file just i have explore.php . I used rel="canonical" before it is working fine but now it is not working. – AlwaysStudent Jun 24 '16 at 19:16
  • i used this think : https://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/ from bottom area in this website – AlwaysStudent Jun 24 '16 at 19:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115554/discussion-between-anubhava-and-devstud). – anubhava Jun 24 '16 at 19:21
  • @anubhava I have a question from you, what should i do to work you answered htaccess codes both (subfolders,subdomains and root folders) ? Because for example: `RewriteRule (?:^|/)([\w-]+)/?$ profile.php?username=$1 [L,QSA]` is not working in subfolder ? I need help about this – AlwaysStudent Dec 13 '17 at 08:32
  • That rule would work in sub-folder also. Just make sure you comment out `RewriteBase /` line for `subfolder/.htaccess` – anubhava Dec 13 '17 at 08:44
  • @anubhava Do you mean i need to change `RewriteBase /` to `RewriteBase subfolder/` ? If yes that means we should almost write subfolder name in htaccess ? – AlwaysStudent Dec 13 '17 at 08:49
  • If .htaccess is inside the subfolder then it is good to have appropriate `RewriteBase` set. – anubhava Dec 13 '17 at 09:15