Help with .htaccess mod_rewrite

oracle

New Member
Messages
430
Reaction score
0
Points
0
Hi,

I am using rewrite rules on my website. Something like this for example:

Code:
RewriteRule ^jobs jobs.php
RewriteRule ^jobs/ jobs.php

Now this works perfectly, http://mysite.com/jobs works as http://mysite.com/jobs.php

However when I try to add the following:

Code:
RewriteRule ^jobs jobs.php
RewriteRule ^jobs/ jobs.php
RewriteRule ^jobs/([0-9]+)$ jobs.php?q=$1
RewriteRule ^jobs/([0-9]+)/$ jobs.php?q=$1

So that somthing like: http://mysite.com/jobs/1 works as http://mysite.com/jobs.php?q=1 , the url rewrite i guess works correctly but the internal includes like:

include_once("header.php");
<script src="js/script.js'></script>

goes haywire. Probably because they thing they are insite directory "1".

I am unable to understand how can I solve this issue. Probably supllying full url for include files and js files will work. But the X10 have disabled including files with http:// in them i guess.

Does any solution exists??

Imoracle
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
Well first of all, you would use the document root global variable in php to specify a local absolute path:

include_once($_SERVER['DOCUMENT_ROOT'] . "/header.php");

And second of all, x10 does not and (for the most part) cannot stop you from including files using an absolute url in your html. The client is responsible for accessing files referenced in the html, so it poses no security risk to x10. But you don't need to use an absolute url if the file is on your server. Just prepend a forward slash to the path to begin from the root, like this:

<script src="/js/script.js"></script>

which is equivalent to this regardless of the current directory:

<script src="http://your.site.com/js/script.js"></script>

As for your rewriterule's, I can understand them messing up relative paths of files in your html, but I don't see why they should mess up include()'d files in php. However, your rules do contain a small bug. Because you don't have an ending $ in the first rule, none of the other rules will have any effect. Try this instead:

Code:
RewriteRule ^jobs/?$ jobs.php [L]
RewriteRule ^jobs/([0-9]+)/?$ jobs.php?q=$1 [L]

A question mark following a character means that the character is optional. This can be used here instead of making an additional rule. And the [L] after the rule just means to apply no further rules to the url -- it's the Last rule. For efficiency, it's good to add that on to any rule that should be the only or final rule to apply to a certain url.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
As an alternative to the RewriteRules, you could use MultiViews (part of content negotiation and $_SERVER['PATH_INFO'] in jobs.php rather than $_REQUEST['q'] (though you will have to process the slashes out of PATH_INFO). Then you have no problems with relative URLs in the generated webpage.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
As for your rewriterule's, I can understand them messing up relative paths of files in your html, but I don't see why they should mess up include()'d files in php.

The rewrites will mess up includes if they're handled by Apache subrequests. And easy fix to try is to add a RewriteCond to skip subrequests before the RewriteRule(s):
Code:
RewriteCond %{IS_SUBREQ} false
RewriteRule ^jobs/?$ jobs.php [L]

RewriteCond %{IS_SUBREQ} false
RewriteRule ^jobs/([0-9]+)/?$ jobs.php?q=$1 [L]
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
This topic is pretty old, but regardless php doesn't use apache subrequests for include(). The only function I can think of that does is virtual().

Furthermore, it would be easier to use the NS(no subrequest) flag on the rule rather than to create a condition to void it on subrequests. E.g:

RewriteRule ^jobs/?$ jobs.php [NS,L]
 
Top