Jump to content

PHP and ModRewrite


saf

Recommended Posts

I am trying to get clean urls (www.site.com/one/two instead of www.site.com/index.php?1=one&2=two) using modrewrite and php and it works fine when I use it from the root directory but crashes when I put all my files (including .htaccess) into a subdirectory.

 

Here is my code:

/test/.htaccess

RewriteEngine On 

RewriteCond %{SCRIPT_FILENAME} !-f 
RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteRule ^([^/]+)/?(.*)?$ /test/index.php?one=$1&two=$2

 

/test/index.php

<?php

if(isset($_GET['one']))
{
print $_GET['one']."<br />";
print $_GET['two']."<br />";
}

print "<a href=\"/projects/1\">project 1</a><br />";
print "<a href=\"/settings/5\">settings 5</a><br />";
?>

Link to comment
https://forums.phpfreaks.com/topic/37488-php-and-modrewrite/
Share on other sites

  • 2 weeks later...

I believe it works relatively. Try removing the /test from your .htaccess file.

 

Thats not the real problem

 

Problem Scenario

1. Place the .htaccess file and the index.php file in a directory called test

2. Call the index.php file in the browser

3. Hover on the links and look at the path in the status bar

4. Now click on one of the links

5. Hover over the links on this "new" page and look at the status bar

6. Click on one of the links

 

Analysis

Whats happening is that the links on the first page generate a the url such as http://localhost/test/project/1 and when you click on the next link (on this "new" page), it assumes that it is in the "project" directory and generates the url such as http://localhost/test/project/settings/5 instead of http://localhost/test/settings/5.

 

Probable solution

One solution is to use absolute URLs instead of relative URLs. I tried to do that and it worked...I also tried to pass the path to the directory through a define statement and use that in the links and it also worked, but I am trying to avoid the situation of having to code the path because my project is ment to be distributed and I dont want to have anyone going in and having to change code.

 

Question

Is there any way I can tell the .htaccess file to have all links start from the .htaccess directory? (basically if the .htaccess is in the test file, all links will automatically start @ http://localhost/test/ no matter what the case)

Link to comment
https://forums.phpfreaks.com/topic/37488-php-and-modrewrite/#findComment-185702
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.