开发者

Adding mod-rewrite to an existing PHP website

开发者 https://www.devze.com 2023-01-17 16:09 出处:网络
I am updating a php app, which currently does not use url rewriting. The aim is to hide file extensions. The general structure of the website is as fol开发者_如何学运维lows

I am updating a php app, which currently does not use url rewriting. The aim is to hide file extensions. The general structure of the website is as fol开发者_如何学运维lows

root/
  index.php
  login.php
  page1.php
  page2.php
  page3.php
  page4.php
  page5.php
  page6.php
  page7.php
  page8.php
subfolder/
  index.php
  validpage.php
images/
css/

The subfolder folder in the above structure is unique in that it is the only subfolder containing php files.All of the other files are in the root folder.

I have been through these questions Mod Rewrite and PHP and Mod-Rewrite or PHP router? and mod_rewrite, php and the .htaccess file

Updated htaccess. thanks to @thomasmalt

RewriteEngine on
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -l 
RewriteRule ^.*$ - [NC,L]


# only rewrite if the requested file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-s 

# add .php to the url     
RewriteRule ^(.*)$ $1.php

Problem now is I get a 500 instead of a 404 error, when I try to access a non existent page like localhost/inex.php. The Apache error log gives me the following errors. (I accessed localhost/index which loaded the contents of index.php , followed by which I loaded localhost/inex which gave me a 500 error.)

[Sat Sep 25 14:44:26 2010] [error] [client 127.0.0.1] File does not exist: C:/wamp/www/index
[Sat Sep 25 14:44:36 2010] [error] [client 127.0.0.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.

Update: found the answer to my question here. The question is resolved.


Apache has an option: Options +MultiViews

If you enable this, you don't need to place the file-extention in the url.


I'm not totally sure what you're trying to achieve, and i suspect, no disrespect intended, that you need to understand both mod_rewrite, and the way the webserver and php parses and executes files. I'm not sure how to answer without shooting over or under your level of understanding, but I'll try.

I can tell you what you're current .htaccess is doing: (pseudocode)

if REQUEST_FILENAME does not exist then
    rewrite request to /index.php/<the request>

Since index.php is your php script that is executed and everything put in $1 is ignored. That's why index.php is executed whatever you do.

What you could try to put in your .htaccess is this:

RewriteCond %{REQUEST_FILENAME} !-s 
RewriteRule ^(.*)$ $1.php

That should take the request and try to append .php to the end of it. I haven't tested it unfortunately, but you should give it a try. The result should be something like this:

if request is "/news/article" 
    translate it to "/news/article.php"

And you should read the mod_rewrite documentation twice a day for a month, and buy the O'Reilly book about Regular Expressions.

And if you have administrator access to the development server you should look into

RewriteLogLevel
RewriteLog


From experience, it's harder than it sounds to retro-fit mod_rewrite into an existing site.

This isn't because mod_rewrite is hard -- actually it's relatively easy to set it up and get your new URLs working. The reason it's hard is because your existing site will have all the old URLs hard-coded into it, so you'll have to go through your code changing them all to point to the new URL format - this can be especially tricky if your code builds up URL strings dynamically. You'll need to do lots of testing to make sure you've picked up every possible variation and that they all still work.

Also worth mentioning that search engines and external sites will have historic links in to your site using the old URLs, so you can't get rid of them completely.

I know that doesn't answer your question, but I believe these are important things to consider before you start the project, because for an existing site it isn't the quick-fix that some people make it out to be.


  1. It send you to root because you have RewriteBase / what defining default base ... If you want to stay in subfolder just make another .htaccess in subfolder with RewriteBase /subfolder/ or remove it
  2. you can make condition like you want

    RewriteRule ^(([0-9]+)-)?page(-([0-9]+))?/?$    index.php?page=$2&id=$4 [L]
    

this will take you from www.abcd.com/2-page-3 to www.abcd.com/index.php?page=2&id=3

  1. Code in index.php doesn't need to contain anything.
0

精彩评论

暂无评论...
验证码 换一张
取 消