Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 665
  • Last Modified:

PHP Include with URL Rewrite

Hi,

I have a website with a main category.php file.

In category.php, a few style sheets are linked to this page as well as a few php include statements;
ie. <link href="css/sheet.css" rel="stylesheet" type="text/css" />

Now... When the user selects a link from the menu it goes to category.php and loads dynamic content from the database. I have a .htaccess file that rewrites the url string to put certain links into different "virtual" directories.

For example, if a user clicks on wine > shiraz in the menu, the url string will look like; www.domain.com/wine/wine-name
or
if the user clicks on location > australia > perth in the menu;
www.domain.com/location/australia/perth

This works perfectly, the main problem is that my style sheets and other php includes are now not loading into the category.php page.

Can someone direct me to the best possible solution... so i can use my php includes and link the stylesheets and everything loads regardless of how "virtual" directories the url rewrite makes.
0
JiveMedia
Asked:
JiveMedia
  • 6
  • 5
1 Solution
 
sudhakarsp06Commented:
Make sure that your htaccess rules ignore css, js requests so that those resources would be loaded from the place where it is physically. Or if you do not mind please share the htaccess file ( after removing the senstive data ) I will guide you.
0
 
V4nP3rs13Commented:
hmmm.. if I understand you, you will have to put direct link to your stylesheet... like this:
<link href="http://example.com/css/sheet.css" rel="stylesheet" type="text/css" />

because, if the link is css/sheet.css, then, the css link will be www.domain.com/wine/wine-name/css/sheet.css, but it isn't correct

I hope this will help
0
 
sudhakarsp06Commented:
Hi V4nP3rs13:

Probably he should have directed all the request to categories.php file thats why the style sheets are getting loaded, even if you give the correct resource path, this request will hit the categories.php file. Thats why we need to negate the css and js request in htaccess file itself.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
JiveMediaAuthor Commented:
Hi again,

Please see code for the htaccess file.
The url rewrite also breaks the links in my php includes, for example;

<?php include('menu.php'); ?> and <?php include('modules/').$row_rs_category['category_module']; ?>

would usually load because the file/folder are in the same dir, but now its broken because of the added rewrite directories.


Options -MultiViews
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^([a-z0-9_-]+)$ category.php?ref=$1
RewriteRule ^([a-z0-9_-]+)/([a-z0-9_-]+)$ category.php?ref=$1&item=$2
RewriteRule ^events/([a-z0-9_-]+)$ events.php?ref=$1

Open in new window

0
 
sudhakarsp06Commented:
This rewrite rule will not break any thing in php include part, because understand that php would start execution after the apache requesting page. So only the request to the server will look into the htaceess not the server side includes.

Also I tested the above code it is working fine in my system, css and js are loading. May be u need to rearrange the order of the rules like this below

RewriteRule ^([a-z0-9_-]+)/([a-z0-9_-]+)$ category.php?ref=$1&item=$2
RewriteRule ^([a-z0-9_-]+)$ category.php?ref=$1
RewriteRule ^events/([a-z0-9_-]+)$ events.php?ref=$1

Please let me know if u have any questions.
0
 
JiveMediaAuthor Commented:
Hi sudhakarsp06,

I tried rearranging the rules, but still doesn't seem to be working.
I get these 2 warnings. Works perfectly locally on my imac, when i upload to file server doesn't want to play :(

Warning: include(modules/) [function.include]: failed to open stream: No such file or directory in /home/upperreach/www/www/category.php on line 146

Warning: include() [function.include]: Failed opening 'modules/' for inclusion (include_path='.:/usr/local/php5/lib/php') in /home/upperreach/www/www/category.php on line 146

Anyone have any ideas?
0
 
JiveMediaAuthor Commented:
Got this as well...

Not Found

The requested URL /about/function.include was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
0
 
sudhakarsp06Commented:
Hi

First please check mod rewrite is enabled in the apache configuration file. Only if the mod rewrite is not enabled this .htaccess stuff won;t work.

Also see the warning message, it looks like the file u r trying to include is missing, see the below code marked with {x}, hence u have not included any file, but only the folder. Please have a look into this and try to get the value of the file after the folder name.

Warning: include(modules/{x}) [function.include]: failed to open stream: No such file or directory in /home/upperreach/www/www/category.php on line 146
0
 
JiveMediaAuthor Commented:
The htaccess rewriting of the url works fine, its the stylesheets and includes that are not working :(
All the files are on the server too. It has me stumped.
0
 
sudhakarsp06Commented:
what is the output if you hit the css request?
 also tell me what is the piece of code in line number 146 in category.php
0
 
JiveMediaAuthor Commented:
Got the stylesheets working, one problem down :)

line 146 is <?php include('modules/'.$row_rs_category['category_module']); ?>

The main dir is where category.php and the modules folder are located.
Inside the modules folder, are a few php documents that are loaded dynamically from the database through the include depending if they are true or false.
Seems to work on my local computer but not the web server.

It pulls a filename from the category_module column in the database then adds it to the url after modules/ in the include statement.

I get these 2 errors.

Warning: include(modules/) [function.include]: failed to open stream: No such file or directory in /home/upperreach/www/www/category.php on line 146

Warning: include() [function.include]: Failed opening 'modules/' for inclusion (include_path='.:/usr/local/php5/lib/php') in /home/upperreach/www/www/category.php on line 146
0
 
sudhakarsp06Commented:
I believe there is some data is missing in the line

line 146 is <?php include('modules/'.$row_rs_category['category_module']); ?>

simply echo the array $row_rs_category['category_module'] and check if u r getting the data in it.
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now