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
1.2k views
in Technique[技术] by (71.8m points)

apache - mod_rewrite for all pages (including subfolders) on a site to a single php page

I am in the process of converting my site with many static html pages to a site driven by a database. My problem is that I don't want to lose what google has already indexed, so I would like to rewrite requests to be sent through a php script which lookup the filepath for content in the database. My understanding is that a mod_rewrite would best serve this purpose, unfortunately I have never used it, so I am a bit lost.

What I have:

www.domain.com/index.html
www.domain.com/page.html?var=123&flag=true
www.domain.com/folder/subfolder/
www.domain.com/folder/subfolder/index.html
www.domain.com/folder/subfolder/new/test.html
www.domain.com/folder/subfolder/new/test.html?var=123&flag=true

What I want (I also probably need to urlencode the path)(passing the full uri is also ok):

www.domain.com/index.php?page=/index.html OR www.domain.com/index.php?page=www.domain.com/index.html
www.domain.com/index.php?page=/page.html?var=123&flag=true
www.domain.com/index.php?page=/folder/subfolder/
www.domain.com/index.php?page=/folder/subfolder/index.html
www.domain.com/index.php?page=/folder/subfolder/new/test.html
www.domain.com/index.php?page=/folder/subfolder/new/test.html?var=123&flag=true

Here's my first go at it:

RewriteEngine On  # Turn on rewriting    
RewriteCond %{REQUEST_URI} .* # Do I even need this?    
^(.*)$ /index.php?page=$1

Ideas? Thanks in advance :)

Edit:

So I tried implementing Ragnar's solution, but I kept getting 500 errors when I use 'RewriteCond $1' or include the '/' on the last line. I have setup a test.php file which will echo GET_["page"] so I know that the rewrite is working correctly. So far I can get some of the correct output (but only when I am not in root), for example:

RewriteEngine on
RewriteRule ^page/(.*)$ test.php?page=$1 [L]

If I visit the page http://www.domain.com/page/test/subdirectory/page.html?var=123 it will output 'test/subdirectory/page.html' (missing the querystring, which I need). However, if I use this example:

RewriteEngine on
RewriteRule ^(.*)$ test.php?page=$1 [L]

If I visit http://www.domain.com/page/test/subdirectory/page.html?var=123 it will only output 'test.php' which is thoroughly confusing. Thoughts?

Edit #2:

It seems I've been going about this all wrong. I just wanted the ability to use full uri in my php script page. The final working solution to do what I want is the following:

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /test.php

Then in my php script, I can use $_SERVER['REQUEST_URI'] to get what I need. I knew this should have been easier than what I was trying...

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

I would recommend you to look into the Apache URL Rewriting Guide, it contains extensive information about rewriting with examples.

If I understand you correctly, you should be able to use something like this

RewriteEngine on
RewriteCond $1 
RewriteRule ^(.*)$ index.php/?page=$1 [L]

Which is very similar code to the one you posted. If you want better information, be specific about your problem.


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

...