Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with HTAccess 1

Status
Not open for further replies.

theniteowl

Programmer
May 24, 2005
1,975
US
Hi All,
I am re-vamping the local middle school web site and would like to have all links from the site piped through my template page so as to enforce a common template to the site but still allow the teachers to create their own pages that will get loaded inside that template.

I am told that using HTAccess should allow for this.
What I need is to have all links in pages under this set of folders to automatically load via my template unless the link is for a site not local to the site in which case I would want it to load into a new browser window.

Can anyone give me a basic rundown on how to set this up? I have read around a bit but am still putting puzzle pieces together from different sources.
A basic example of how to setup to force page xyz.htm to get loaded through the main index.php page should be all I need to figure things out. And if there is any server-side information available such as the folder location of the page the link was called from or other such useful bits of info that would be enormously helpful.

Essentially I have a main page that loads primary and secondary navigation controls based on current page selection and then loads the appropriate html page within the main content window. It works well as is except that if the teachers create an HTML page with links they currently would have to specially forumalate their links to pass values to my script page and I do not want to have to rely on them having to remember how/when to do that.

Thanks.


[blue]It's hard to think outside the box when I'm trapped in a cubicle.[/blue]
In an increasingly self-focused society it is important to recognize the unselfish actions of others so they will feel encouraged to continue such actions. Please give acknowledgement to those who aid you whether it is waving to the person who let you out
 
If you have a PHP question, please explain what it is.

If you are asking about Apache functionality, I recommend that you post it in the Apache forum: forum65



Want the best answers? Ask the best questions! TANSTAAFL!
 
Well, it was in this forum that the suggestion to use an HTAccess file was originally made so not being familiar with HTAccess files I guess I just mentally associated them with PHP. Come to think of it, it does make more sense that it would be related to the web server but since Apache is not the only server that runs PHP scripts it would not have occured to me to ask in the Apache forum.

So now that I round-aboutly have a better idea of where to ask if not necessarily what, I will go there and do so.

Thank you for the reply it was informative and was probably not meant to be nearly as terse as it sounded.


[blue]It's hard to think outside the box when I'm trapped in a cubicle.[/blue]
In an increasingly self-focused society it is important to recognize the unselfish actions of others so they will feel encouraged to continue such actions. Please give acknowledgement to those who aid you whether it is waving to the person who let you out
 
yourdictionary.com defines terse as "brief and to the point; effectively concise".

Yeah, that's pretty much what I posted.



Want the best answers? Ask the best questions! TANSTAAFL!
 
I guess you would have to consider it in context then. It is often used to imply not only brevity but shortness of temper. Using the literal definition of yourdictionary.com from the perspecitve of your answer then it certainly would be concise from your perspecitve as it was certainly the point you were trying to make though not quite a concise answer to the question posed. But now it's just a matter of semantics and unknown intent really.

So I will work on the assumption that your answer really was intended only to be helpful and not to sound short tempered. And if I am wrong on my assumption then have a nice day anyway cause you obviously are here to try and help out where you can as am I, though not in this particular forum, and a certain amount of understanding can be afforded given the service freely offered.



[blue]It's hard to think outside the box when I'm trapped in a cubicle.[/blue]
In an increasingly self-focused society it is important to recognize the unselfish actions of others so they will feel encouraged to continue such actions. Please give acknowledgement to those who aid you whether it is waving to the person who let you out
 
@theniteowl

i think you are looking for mod_rewrite. this is only available on apache (sfaik) so if you want to pursue this route then the apache forum is your best route.

there are alternative structures you might consider:

1. as a "quick and dirty fix", move all the existing web files to another directory, customise the 404 page not found error to point back to your newly located index.php file. in that file analyse the requested link and either route it through your index.php file (rewriting the links dynamically) or direct the user to another site.

2. submit the whole site to a content management system. this is the better solution but i would guess will require a lot of upfront investment in time.

 
I am creating a psuedo content management system for them in PHP to keep the site structure and navigation consistent.
They need to be able to create and place their own pages or at least they have the ability to do so and FTP access to the server to place their files. I am only helping them out getting their site re-written and have no way of enforcing a structure on them if they choose to go around it so the easier it operates the better.

I first considered parsing out the pages being included into the content window and altering their links before they are rendered but was told that the htaccess file could handle redirection of the links for me and simplify things.

I could build a page upload form that they submit new pages through and modify them once uploaded but that complicates the system, requires a lot of work and they could bypass it just by doing things the way they are used to.
My goal is to not require any major participation on their end for the system to work. For pages with frequently updated info I can build individual page content management forms but do not want to require those forms for every possible page they put out there.

I only have FTP access to the server, the server is owned by an independent company and their client is the local school board not the individual school this site is for so getting any server-side changes made is a major hassle as the provider will not talk to me directly nor to any member of the school this site is for. And if minor server-side configuration changes are difficult, getting any kind of content management system setup would be impossible.

Gee the things I get into when I volunteer my time. My own projects are not usually this complicated. :)


It's hard to think outside the box when I'm trapped in a cubicle.
 
can we backtrack a bit?

does it solve your problem if all requests for every page are diverted to your index.php page? understanding your constraints i think that this can be achieved.

i am not sure how mod-rewrite can be used to fix the problem but this could well be that i don't have a full grasp of what you are actually trying to achieve. could you provide a url so we can see what is going on? and it would help if you could point out which bits of the site are editable by users (i guess they can't wholesale upload pages as there would be no linkage).
 
No available url at the moment, I have it running on my IIS server at home with PHP installed but it is not exposed to the outside.

Essentially this is what I have.
The index.php page has a bit of code to parse out parameters on the URL into variables on the page that dictate the current state of the menu. The menu code is another include file that generates based on values in a text file (no DB was available when I created it) that states top level options and associated second level options.
In the body of the page I include a page header file that contains the standard header for the site and the top level of the navigation menu. Then I include the second navigation level, and finally an include for the content portion of the page.
It is pretty compact, just a small amount of code at the top of the index.php, the include containing the logic for nav menu and the body which has the three sections header, second nav menu and finally content.

The goal is to have one simple form to maintain the navigation menu controls. The structure of every page will be the same except for the content window.
The current selection of the top nav dictactes which sub nav options display which in turn dictates which content page get's included.

Everything works great except that if the creator of the content page puts in a hyperlink to somewhere on the same web site they would have to specially format the link to point to the index.php file setting URL parameters for which page to load like this:
<A HREF="<?= $linkurl ?>mysubfolder/mylink">My Link</a>
Where $linkurl is a variable already built by the previous page code and consists of the relative path to index.php followed by whatever parameters are passed for the current first and second level nav options and name of the page to load if it is not the default page for the current nav options.

When I have to write it out it sounds more complicated than it is. The point is that in order for a link on a content page to not end up breaking out of the templated design it has to not actually load the page but to tell the index.php page what to load.
It works well but it can be difficult for the teachers who would only infrequently use it to remember to modify their links and how to modify them correctly. It also requires that the content page they create be based on a template file I provide them not just any old html file they come up with or we end up with a messed up table structure (working to get rid of tables but it takes time) or two sets of html, head, body, etc tags.

I could try to use code to alter the links dynamically or find a way to redirect to my own script to test the link and determine if it should be processed through the template or if it is an external link that should be let through.

The whole site is designed strictly with PHP with almost no graphics though it does present itself as if it uses a lot of graphics. I aim to make it as cross-browser friendly as possible and able to be used by any softwares designed for special needs access like screen readers, magnifiers, etc.
It is designed to allow for easy alteration of color schemes and even page layout for the entire site if they want to change their look without having to change the script.
When I am done I want the site to be easily kept up by the school so they do not have to rely on me to change things for them all the time or have to know server-side coding to keep it running.

I have very lofty goals for myself and to make it worse I had never even looked at PHP before I started this so I am learning it as I go and there may be easier methods to do some of the things I am doing but I am not aware of them yet. :)

It's hard to think outside the box when I'm trapped in a cubicle.
 
Is there an easy method to identify the servers software?
I assume it is probably Apache but do not know for certain.
I have been trying out various bits of code to send form results as emails from their server also but so far no go.
Not sure if they have anything on the server to handle routing the email setup or if they have it locked down somehow so that it has to be enabled on the server before I can send anything from my folders.



It's hard to think outside the box when I'm trapped in a cubicle.
 
Is there an easy method to identify the servers software?
use phpinfo(); it will report the server version.

I've got a handle on your needs now. i'm not convinced that mod_rewrite does it for you. i think my idea is probably neater.

almost certainly your server will support custom error pages so all you need to do is put some code in your index.php file that tests for the requested uri and remaps the requested link with your queryparameter based paradigm. promise this is not difficult! just load the following into a text file, upload it to your web root and rename the file with ftp as .htaccess
Code:
ErrorDocument 404 /index.php?error
this means that any broken link will be pushed back to index.php

then test and manipulate the requested url
Code:
if (isset($_GET['error'])):
 echo "requested uri as {$_SERVER['REQUEST_URI']}";
endif;
 
Maybe I just do not see it yet but I do not think this will suit my needs.
The issue for me is not broken links but actual valid ones.

In the normal course of events the index.php file will setup the menu and then include the appropriate content page. If that content page happens to have a hyperlink pointing to another page, say one even in the same folder but the link is <a href="somepage.htm">Some Page</a> then that page would load in the browser without being loaded inside the template. To make it load inside the template the link would have to read <a href="<?= $linkurl ?>somepage">Some Page</a> which sets the link as index.php passing the page name as a parameter so the index.php knows which page to actually load.

If I understand what you are saying above it would only work in the instance that someone tried accessing a page inside the site with a link that did not work.

My goal is to not have to make them re-formulate their links but instead to intercept the links passing them to a script on my site that will evaluate the url and handle the page load appropriate to the template.


It's hard to think outside the box when I'm trapped in a cubicle.
 
The issue for me is not broken links but actual valid ones.

sorry - wasn't clear. you force the links to be broken by moving the files to a different drectory. or alternatively change the error code to 500 and make all of the sub-directories denied to users in the htaccess file.
 
Moving files would not work well in this case because it would be other people FTP'ing the files to the server but altering the access to the sub-directories might work.
It could be a little confusing down the road if someone else tries to do work on the site and cannot figure out why they cannot access the files in those folders directly but it sounds like it could work with just a bit of reworking of my code.

I hope to get some time to do some testing tonight or tomorrow morning, hopefully the server has opened up access to using htaccess. I would assume so since it is a hosting server with a bunch of unrelated web sites there.

I put a lot of effort into setting up the code to get to where I am now but if you think another approach would be better I am open to ideas. It started out as a menu system to use across all the pages and have the sub menu alter it's options based on the current top level selection and to have the menu dynamically generated from a list so that maintenance on the menu could be simplified. Passing values on the URL rather than storing in session variables enabled people to save a link that would always bring up the target page where using session variables would leave a URL that would always start them at the home page. In some ways that would be nice to make sure they always see any new home page content but if I were a regular visitor it would annoy the heck outa me so...

Thanks very much the help.


It's hard to think outside the box when I'm trapped in a cubicle.
 
I put a lot of effort into setting up the code to get to where I am now but if you think another approach would be better I am open to ideas.

nope - thing the approach is fine. leads itself to a despatch based solution which is what i favour.

Passing values on the URL rather than storing in session variables enabled people to save a link that would always bring up the target page where using session variables would leave a URL that would always start them at the home page.

agreed - of course the solution discussed above allows links to be saved in either the "good" format or the bad one.

post back if you have problems!
 
Hey, good news/bad news. I got the htaccess file to work redirecting to my index.php file, the only problem is that the URL does not change and when the index.php file runs it keeps the path of the test file which kind of screws up the rest of the application which is relying on relative paths.

How in PHP can I send it to the location I need it to be?
It occurs to me also that if I no longer build the URL for the link to pass the menu parameters I have to reconsider how the menu is going to work.


It's hard to think outside the box when I'm trapped in a cubicle.
 
give me an example of a url that is called by a rogue page and what it actually should look like.

i don't think your last concern is real - unless i am still misunderstanding your app!
 
When a link on the menu is called it has already appended the info so that the receiving page knows how to set highlighting on the top nav menu, which menu options to show for the second level nav and whether any highlighting should be done there.

Links from a content page should just be normal links with either a relative path to the file or a full URL if it is going somewhere else. The current design required specially creating the link so that it pointed to index.php, added in the menu params and passed the name of the file to load as the last parameter. I hope to get rid of the special requirements for the content page links using the htaccess file so they will just be typical hrefs.

What I did was create a sub folder from my web folder called test and put the .htaccess file in there with these lines:
Code:
ErrorDocument 403 /newjfk/index.php?error
<limit GET>
deny from all
</limit>
Then I typed in a URL that would call page that existed in the newjfk/temp folder to test the error handling.
It blocked access to the page and called the index.php page but when the page loaded not everything got included.

It got all of the page includes but left out all of the images and possibly the stylesheet. The URL of the browser remains pointing at the subfolder path even though the index.php loaded and the links are operating relative assuming the root path which is why I think the images did not load.
I think what I need to do is when the error page runs, detect what path the call was to, set appropriate URL values for the menu and redirect to my template file so the browser URL changes.

Can PHP do a redirect like this or does it have to be done client-side? So far I have avoided any javascript for navigation so the nav system will not get broken if the user has disabled javascript.


It's hard to think outside the box when I'm trapped in a cubicle.
 
It got all of the page includes but left out all of the images and possibly the stylesheet. The URL of the browser remains pointing at the subfolder path even though the index.php loaded and the links are operating relative assuming the root path which is why I think the images did not load.
it would if the images were in one of the folders that were "denied". you can get around this with having all images stored in another folder that is not denied.

I think what I need to do is when the error page runs, detect what path the call was to, set appropriate URL values for the menu and redirect to my template file so the browser URL changes.
Can PHP do a redirect like this
yes! that was what my posted code did. the actual url that was requested should be stored in $_SERVER['REQUEST_URI']. you need to capture this and parse it into the form that you want. I can help with this but need the information that I requested in the previous post.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top