Forum Moderators: phranque

Message Too Old, No Replies

index.html to page.php?fn=index

Using Rewrite to reference php code

         

fsp412

2:05 pm on Apr 16, 2019 (gmt 0)

5+ Year Member



I was able to do this with an old site, but lost the files. I am trying to reference file names to a PHP script. An example will be index.html to reference page.php?fn=index while still keeping index.html in the URL. Obviously, other pages will be done similarly. Any assistance will be greatly appreciated.

lucy24

7:44 pm on Apr 16, 2019 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In the full realization that this is not the question you asked...
while still keeping index.html in the URL
Why, for heaven’s sake? The two options are almost equally bad.

Or are you using “index.html” as a for-posting-purposes standin for a variety of URLs, like
/abc.html >> /page.php?fn=abc
/def.html >> /page.php?fn=def
et cetera?

fsp412

8:02 pm on Apr 16, 2019 (gmt 0)

5+ Year Member



Yes, the idea will be to hid page.pho?fn=abc by using abc.html. The HTML file name is the same as the fn variable in the page.php. That way, if someone goes to abc.html, the page.php will take the file name and display the appropriate content.

lucy24

8:38 pm on Apr 16, 2019 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Whew. In that case, it's a single conditionless RewriteRule ([L] flag only)--probably exactly what you did before you misplaced the old htaccess file.

What have you tried so far? There are basically two ways to do it: one by passing the pagename explicitly to the php as you’ve already shown, and another by rewriting to “page.php” without parameter, and then letting the php itself fish out the URL. Most people do it the first way; I don’t know if there’s a major difference in server workload.

Oh yes and...
The HTML file name is the same as the fn variable
When you say “HTML file name” do you actually mean the visible URL? I can think of situations where both would come into play--say, “abc.html” is really a php file that does various stuff and also includes the content of a file that coincidentally happens to be named “abc.html”.

fsp412

8:47 pm on Apr 16, 2019 (gmt 0)

5+ Year Member



I hadn't tried anything yet. I am still working on writing some basic content, and CSS for a test site. Before, the .php file would use the URL to find the filename. So for example, http://example.com/abc.html will show in the address bar of the web browser. page.php will fish out the abc and use that in the page.php?fn=abc. The page.php does all of the heavy work, but never shows in the address bar. Hope that clarifies everything.



[edited by: not2easy at 3:30 am (utc) on Apr 17, 2019]
[edit reason] 'example.com' for readability [/edit]

phranque

9:42 pm on Apr 16, 2019 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], fsp412!

I hadn't tried anything yet.

be sure to read the Apache Web Server forum Charter [webmasterworld.com] and please show some work.

fsp412

2:18 pm on Apr 20, 2019 (gmt 0)

5+ Year Member



#Show .html file, but use page.php
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^([0-9a-zA-Z_-]+).html page.php?fn=$1 [NC,L]

lucy24

5:11 pm on Apr 20, 2019 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What’s the [NC] flag for? Do you anticipate users requesting
pagename.HTML
? Casing of the captured part doesn’t matter, since your pattern explicitly said both A-Z and a-z.

In the pattern it should be \.html with escaped literal period, but this is a non-lethal error; it would only come into play if someone accidentally requested “pagenamehtml” without the dot. Mercifully this is not something search engines do on purpose.

Always, always, always start your rewrite target with a / as this protects you against malign operators sneaking in with “all your RewriteBase are belong to us” code. (Yes, / is the default. But why take chances.)

Personally I would express the pattern as
^([^./]+)\.html
at a savings of eight bytes, but that takes us into personal-coding-style territory.

fsp412

5:41 pm on Apr 20, 2019 (gmt 0)

5+ Year Member



Lucy,
As for the NC, just trying to cover my basis especially since I couldn't get things working. Thank you for the code correction, everything seems to be working fine now. And thank-you for the suggestion on the reduction of code. That will be used as well.


Frank