9

EDIT: current .htaccess file:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension snippet

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

My site is hosted in a subfolder of a domain connected to a large hosting account.

basesite
  /iioengine
    /forums
      /.htaccess //file works
      /.... //other MyBB content
    /demos.php
    /index.php //iioengine.com (homepage)
    /.htaccess //file doesn't work
    /... //other iioengine php pages

Is the issue that I'm using two different htaccess files?

Here is a link that needs to work: http://iioengine.com/demos

I noticed that this current htaccess file disrupts all of the forums URL's as well

This no longer works: http://iioengine.com/forums/Forum-Box2D

EDIT: Thanks for reopening, I have made some progress. Here is my current htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
</IfModule>

I still get 404 pages, but if I put this line in:

RewriteRule . /index.php [L]

all non-'.php' requests get forwarded to the homepage... So mod_rewrite is definitely enabled, it's just not working right. Anyone know what the issue could be?

EDIT: This is not a duplicate - none of the other solutions work for me. My question is not do solutions exist, its why aren't they working for me. No one has been able to resolve this, I have been trying many solutions myself. Isn't the point of this forum to get solutions to specific issues?

Allow me to clarify...

I have MyBB running in a subfolder and its rewrites work fine. This link, for instance, works: http://iioengine.com/forums/Forum-Box2D

All the php pages that are not part of MyBB still have the .php extension in their URLs - I am trying to remove these but nothing is working. Example: http://iioengine.com/demos

... [original post]

There is obviously a lot of information out there about this, but I have tried almost a dozen different solutions and have not gotten past a 404 page.

Here is my site: http://iioengine.com/, all pages are php, and everything other than the homepage and all of the forums pages have a '.php' at the end of their URL that I would like to remove.

In addition to redirecting non-'.php' requests to the correct pages, I would also like to remove the '.php' part even when it is part of the request (because all of my content already specifies '.php' in its hyperlinks).

This is what I have so far, mostly taken from this post, but it doesn't work, I get a 404 page.

RewriteEngine on
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ $1.php [L,QSA]
RewriteCond %{REQUEST_URI} ^/(.*).php$
RewriteRule ^(.*)$ %1 [L,QSA]

what do I need in my htaccess file to remove the file extension from the URL in all cases? Thanks

anubhava
  • 761,203
  • 64
  • 569
  • 643
Cbas
  • 6,003
  • 11
  • 56
  • 87
  • 1
    Maybe this [answer](http://stackoverflow.com/questions/13832468/how-to-stop-htaccess-loop/13832827#13832827) helps. Check option II. – Felipe Alameda A Apr 10 '13 at 05:08
  • MVC, not that I know of. I tried option II - didn't work (still got 404 pages) and it messed up the formatting on all of my pages – Cbas Apr 10 '13 at 05:17
  • I am sorry but I think this question won't be reopened because it is too localized, i. e. a solution for this question won't be helpful for others. StackOverflow is not a support organization, it is a QA site. Questions and answers are most helpful if they are helpful for a lot of people. – nalply Apr 10 '13 at 21:46
  • duh... they reopened it... – nalply Apr 10 '13 at 21:47

5 Answers5

37

Try this code for hiding .php (will work both ways):

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]

## hide .php extension snippet

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • I think there is an issue with how my htaccess file is being read or interpreted. This gives me a 404 page on each request – Cbas Apr 11 '13 at 07:35
  • Pls post your latest .htaccess in your question. Also provide more details like what URI did you try and little bit info about your directory structure. – anubhava Apr 11 '13 at 08:11
  • I think I know what's the problem and it can be fixed. Just need 1 more answer from you. What is DOCUMENT_ROOT for `http://iioengine.com/` and where in DOCUMENT_ROOT you've placed above .htaccess file? – anubhava Apr 11 '13 at 17:44
  • index.php and .htaccess are both in the iioengine folder, which is a direct subfolder of my hosting account's root – Cbas Apr 11 '13 at 18:09
  • But I guess DOCUMENT_ROOT for `http://iioengine.com/` is iioengine folder isn't it? You can put `phpinfo();` in a php file to get that information. – anubhava Apr 11 '13 at 18:27
  • yes it is. here's an info page if it helps iioengine.com/phpinfo.php - I need to remove the htaccess file for you to view it though – Cbas Apr 11 '13 at 18:34
  • Ok got it, I edited my answer based on that `phpinfo()`. Put this edited code in the same .htacess and try it again. – anubhava Apr 11 '13 at 18:40
  • It's definitely removing the extensions, but I'm still getting 404 pages everywhere - including the forums – Cbas Apr 11 '13 at 18:45
  • Made another edit, pls try now. Also it would be better if we move this discussion to chat to sort this out. – anubhava Apr 11 '13 at 18:52
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28055/discussion-between-cbas-and-anubhava) – Cbas Apr 11 '13 at 18:54
  • The previous example works very well when the url ends in a single .php file eg. http://mywebsite/file.php But, it removes everything else when I have additional parameters in the url e.g. http://mywebsite/file.php/param1. Does anyone know how to remove the .php extension, but keep or attach the other parameters? I would like something like http://mywebsite/file.php/param1 to become http://mywebsite/file/param1 – Sergio Oct 10 '13 at 04:01
  • @Sergio: That will require little different approach. Feel free to open a new question and post a link here. Its better not to edit this 2+ yrs old answer. – anubhava Oct 10 '13 at 09:17
3

try this.

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ $1.php [L,QSA]
</IfModule>

if this not work then your sever do not have mod_rewrite activated or support url rewriting.

  • is mod_rewrite on? check `phpinfo()` –  Apr 10 '13 at 05:06
  • 1
    I assume it is because the myBB htaccess rewrites work fine for all of the Forums pages.. ill use phpinfo to make sure – Cbas Apr 10 '13 at 05:09
  • Here's the info page: http://iioengine.com/phpinfo.php. I can't see anything that specifies mod_rewrite – Cbas Apr 10 '13 at 05:15
  • Still didn't work. I'll leave it up for about 20min. I'll look into my server capabilities. – Cbas Apr 10 '13 at 05:42
0

This should work

Options -Indexes +FollowSymlinks -MultiViews

RewriteEngine on

DirectoryIndex index.php

# REDIRECT Force requests for named index files to drop the index file filename, and force non-www to avoid redirect loop
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*index\.(html?|php[45]?)(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]*/)*)index\.(html?|php[45]?)$ http://example.com/$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+)/([^\.]+)\.php\ HTTP/
RewriteRule ^([a-zA-Z0-9_-]+)/([^.]+)\.php$ http://example.com/$1/$2 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+)/([^/]+)/([^\.]+)\.php\ HTTP/
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([^.]+)\.php$ http://example.com/$1/$2/$3 [R=301,L]

# REDIRECT www to non-wwww
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule .? http://example.com%{REQUEST_URI} [R=301,L]

# REWRITE url to filepath
RewriteRule ^([a-zA-Z0-9_-]+)/([^/.]+)$ /$1/$2.php [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([^/.]+)$ /$1/$2/$3.php [L]
Subodh Ghulaxe
  • 18,333
  • 14
  • 83
  • 102
0

Here is the simplest syntax I have found, which would result as below:

RewriteEngine on
RewriteRule ^/?PrivacyPolicy$ legal.php?mode=privacy [NC,L]
RewriteRule ^Report/([0-9]+)$ report.php?id=$1 [NC,L] 

Results:

  1. PrivacyPolicy to legal.php?mode=privacy
  2. Report/55 to report.php?id=55
Lucas
  • 1,221
  • 9
  • 10
0

On apache 2.4 and later, You can use the following Rule with END flag to remove .php extension from urls.

RewriteEngine on

RewriteRule ^(.+)\.php$ /$1 [NC,L,R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)/?$ /$1.php [END]
Amit Verma
  • 40,709
  • 21
  • 93
  • 115