Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

regex - Remove .php from urls with htaccess

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...