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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automaticaly redirect with HTAccess or mod_rewrite???

Status
Not open for further replies.

theniteowl

Programmer
May 24, 2005
1,975
US
Hi All,
I am trying to find a way to redirect links from pages inside a web site through a template file.

In other words any web page on my site that has a hyperlink, when that hyperlink is clicked I would like to send the request through a template file instead so that any links to pages on the same site are loaded within my template and links to external sites to load within a new window.

I am not familiar with htaccess or mod_rewrite. I have been reading various posts about their usage but nothing so far is specific to what I am trying to do so if anyone can give me some pointers I would appreciate it.

The purpose is to have a templated structure for a school web site, still allow the teachers to create their own pages
but have those pages load in a content window of the template.

Or is there a good way to otherwise intercept the HREF's and alter them as the page loads so I can pass the URL as a value to my template script?
All of the pages will originally come up loaded in the template but if the content page contains links not specifically setup to point to the template file it will break them out of the template and break all the navigation.

Thanks.


It's hard to think outside the box when I'm trapped in a cubicle.
 
Sounds like you want to trap the "Referer" environment variable and direct them to a template page. How does a template page know what content to display? You would have to pass the original request url as a paramater to the template.

Code:
RewriteCond %{HTTP_REFERER}  school-site.com/(.*)
RewriteRule school-site.com/thetemplate.htm?content=$1

If thei applies in only certaain folders you could put it in htaccess, but htaccess overrides have a performance hit.

if you put in httpd.conf you could further qualify the condition url

school-site.com/specialfolder/(.*)

See for more info.

hth
Jeb
\0
 
In the current layout I have a navigation menu system where a click on a top level nav reloads the index page passing a value on the querystring telling it which option was selected and the PHP code sets appropriate options for the sub nav menu based on that info. Clicks on the sub nav menu reload the index page passing both the top and sub nav options which tell the page what options to set and also which content page to load based on the sub nav selected.

I did it this way so that the nav menu code would not rely on client-side script to work and so that the nav menu could be dynamically generated. The menu info comes from an include file that will later have a content management screen for them to make updates to it.
I pass the info on the querystring so that if a person chooses to bookmark a page it is actually bookmarking the index.php page with the parameters required to load the page they want.

This is all so that the site remains within a templated setup. The one drawback is that when the teachers in the school create new content pages, any HREFs within their content page are not going to be formatted to 1. call the index.php page and 2. pass the relevant parameters to set the nav menu options.
I had set up the code to allow them to make relatively easy modifications to their HREFs but in the long run I think having them take special actions for each page they create will cause problems.

If I can cause a redirection to occur I can pipe all page requests through my own template to keep the site consistent.

I Did get this working using a .htaccess file and the assistance of jpadie in the PHP forum. I still have to work on parsing out the requested URL and pass menu options but should be able to get it working.
If there is a better way though I am happy to give it a try.

The one issue is that I do not have access to make any server-side changes, nor would making requests for such changes be likely to be quick if successful at all so anything I can do via my own files is the more likley workable solution.

I can restrict by folder, as this is how I would have to do it with the .htaccess file. I am re-creating the entire site anyway and so can alter the folder structure accordingly.
My experience with Apache and PHP is very limited and I am just learning as I go and I wrote the PHP menu code about one year ago and have to refresh myself on the PHP I used even for that. :)

Would the referer environment variable work reliably for something like this no matter the browser being used or any personalized configurations? I know the info is not always avaialable or can be blocked/spoofed? Perhaps once someone has a page loaded the act of selecting a link inside the same site can give a reliable referer value to work with?

Thanks.


It's hard to think outside the box when I'm trapped in a cubicle.
 
HTTP_REFERER is generally available regardeless of browser. It could be spoofed so you wouldn't want security based on it, but it is good for navigation testing or blocking people from hijacking images etc.

Can you preprocess the content pages with something like sed and rewrite the urls prior to publishing to the server?

Depending on your log settings, referer may be one of the fields in your access_log.

check out
Jeb
\0
 
I am not familiar with sed but the site is one that I volunteered my time to rewrite for the local middle school.
Once my part is done the teachers will have to be able to maintain it themselves. So my goal is to create a site that will be easy for the to maintain but will enforce a bit of structure so that all pages fit into the same framework and navigation scheme but leave them plenty of room for their own individuality on their content pages.

I had this working but in a way that required them to insert a PHP variable onto the beginning of any HREF they put on their own page so it would pass them through the index.php page. Relying on them to do or remember to do this extra step is chancey though so I am looking for more automatic options.
I also do not have any access to the server and any requests for changes have to go through the school board to the districts technology coordinator and then down to the hosting company and as you can imagine it can be difficult to get much accomplished in a reasonable timeframe that way.


It's hard to think outside the box when I'm trapped in a cubicle.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top