2

I'm new to coding Clean URLs, although my educated guess on how they work has been correct.

My first step was to create a compatibility tool to test if mod_rewrite is enabled on the server. I was successfully able to create the three files I needed: the PHP tool file, the .htaccess file and a file URL to be rewritten.

A basic RewriteRule is in place and when I go to the rewritten URL, it successfully redirects. However, upon trying to reference the rewritten URL either by using a PHP INCLUDE/REQUIRE or even a FILE_EXISTS function, it returns unsuccessfully. Is this not possible or am I going about this the wrong way?

I'll be happy to post code if necessary (I'm writing this from my smartphone), but right now I'm still trying to determine if it's even possible and what the best method is to accomplishing it if so.

.htaccess:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteRule .*rewrite_test\.php rewrite.php [nc]
</IfModule>

rewrite.php:

<?php
  $mod_rewrite = TRUE;
  echo "test";
?>

tester.php:

<?php
  // Check if APACHE MOD_REWRITE is enabled for clean URLs
  $mod_rewrite = file_exists('rewrite_test.php');

  echo ($mod_rewrite ? "TRUE" : "FALSE");
?>

or tester.php code alternative:

<?php
  // Check if APACHE MOD_REWRITE is enabled for clean URLs
  $mod_rewrite = FALSE;
  include_once('rewrite_test.php');
  echo $mod_rewrite;
?>
8
  • What does include/require have to do with URL rewriting? Are you trying to include URLs? This is usually disabled by default PHP configuration. Commented Jun 20, 2012 at 12:27
  • Well, when FILE_EXISTS didn't return it, I thought I'd try using an INCLUDE and returning a boolean variable. Apparently that doesn't work. Thanks ;) Commented Jun 20, 2012 at 12:33
  • Perhaps posting some code would help, I still have no idea what you're trying to do. Commented Jun 20, 2012 at 12:35
  • Sure thing...and the goal here is to test via PHP if the current server is capable of mod_rewrite. Commented Jun 20, 2012 at 12:37
  • 1
    Perhaps you would want to use apache_get_modules Commented Jun 20, 2012 at 12:40

2 Answers 2

3

Apache Rewrite Rules don't have any effect on include, require or file_exists as those are using paths on the server, not URLs unless you enable allow_url_fopen and specify a full URL.

EDIT:
if you just want to check if mod_rewrite is available on your server, thats much easier: use apache-get-modules(). it's as easy as:

$mod_rewrite = (
        function_exists('apache_get_modules') &&
        in_array('mod_rewrite', apache_get_modules())
    );
Sign up to request clarification or add additional context in comments.

3 Comments

In essence, I'm using this as a tester for a CMS that might be placed on any server. Thus, I need to know, without permanently changing any Apache or PHP settings if mod_rewrite is enabled. Surely there's a way...surely.
thats actually very easy, just use apache_get_modules. see my edit for more information.
FYI. Only: Restricting access to certain folders (or contents inside that folder) using mod_rewrite (And other options) can obviously stop Ajax calls, or loading html using .load() function of JS. This is the issue i am facing now, hence i think there should be some other way to deal with restriction, in modern apps, using js calls frequently.
0

.htaccess stands for Hypertext Access: it will only affect HTTP requests, not local server requests. As Oezi mentioned, you can use cURL or the allow_url_fopen flag to turn your requests into a true HTTP request, but I'd advise against that as it means you are taking a very circuitous route to the page you want to include. If it's just for testing your .htaccess configuration that isn't an issue, or you could just open the URL in your browser.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.