Jump to content

Mod Rewrite


son

Recommended Posts

I use the following

RewriteRule ^([^/]*)$ /folder/index.php?id=$1 [L]RewriteRule ^([^/]*)/$ /folder/index.php?id=$1 [L]

and it works well. Trying to apply it to the individual file names as

RewriteEngine OnRewriteRule ^([^/]*)/index\.php$ /folder/index.php?id=$1 [L]

crashes the server (500 error). I cannot see what is wrong. I escaped the dot and other than that it should follow the same syntax, shouldn't it? Son

Link to comment
Share on other sites

From my understanding how rewriting works, you're crashing the server because you're telling it to rewrite a URL to something that doesn't match. In other words, this:

RewriteRule ^([^/]*)/index\.php$

Cannot be pointed to:

/folder/index.php?id=$1 [L]

...because in the rewrite URL you're not providing a rewrite rule for the ?id=$1. Hard to explain. I'll give you an example from one of my rewrites:

RewriteRule ^listing/?page=([0-9]+)?$ listing.php?page=$1 [nc,l]

What it means is: If a link on the listing.php page looks like this: listing/?page=5, when that link is clicked, the server translates it to listing.php?page=5. For yours, it would mostly likely have to be like this:

^index\.php?id=([^/]*)?$ /folder/index.php?id=$1 [nc,l]

But that would be like having a regular looking link like: index.php?id=7. The point to rewrite is to have the link written differently than what it actually is. So this would be more appreciate:

^index/?id=([^/]*)?$ index.php?id=$1 [nc,l]

The link on index.php would look like this: index/?id=8 When that ink is clicked on, it will be actually point to index.php?id=8.Since or if the pages are in the root folder for your site, you don't have to have /folder/. I'm not an expert in url rewriting but I saw no responses and I'd figured I'd provide some input. Hopefully it works out for you.

Link to comment
Share on other sites

Hi Don,I see what you are saying, but I try to rewrite, so the folder name is replaced with the content from the query string. For this, your examples would not work...So, as for my task:RewriteRule ^([^/]*)$ /folder/index.php?id=$1 [L]works well to 'create' a folder name from querystring that is used instead of actual folder name. However, my issue is now that when then a user clicks onto any link to files within the folder (there are three, they are index.php, about.php and contact.php) the page cannot be found.Do you have any idea how I could make this work? Thanks,Son

Link to comment
Share on other sites

So are you saying that... within the folder(named 'folder'), there are 3 files called index.php, about.php and contact.php? When someone clicks on a link in any of those pages, for example: In the folder/index.php file, there is a link to folder/about.php file there, it is saying page cannot be found? Is your .htaccess file in the root of the website? With what I have set up, according to what you described, the rewrite rule:

RewriteRule ^([^/]*)$ /folder/index.php?id=$1 [L]

...when accessing the website, the above rewrite rule automatically takes me to the index.php file in the folder named "folder". I set up a link then in that file to the about.php in that same folder. When clicking on that link, it takes me to the same page, which is index.php, but in the browser location bar, what shows up is about.php. So in other words, with the rewrite rule above, it will take any link and always direct you to folder/index.php page.(even when accessing the website) One thing I noticed though... when accessing the website, and when the rewrite looks like how it is above, the forward slash in front of the word 'folder', it doesn't work and I get a page cannot be found. But when removing the forward slash like this:

RewriteRule ^([^/]*)$ folder/index.php?id=$1 [L]

.. it works.

Link to comment
Share on other sites

I can see now where I was going wrong. I created physical pages instead of having one file that has a second parameter for the relevant page. This makes it actually much easier also, so much preferred solution:-) I use now:

Options +FollowSymlinksRewriteEngine OnRewriteCond %{REQUEST_FILENAME} !-fRewriteRule ^([A-Za-z0-9_-]+)$ /folder/index.php?id=$1 [NC]RewriteRule ^([A-Za-z0-9_-]+)/$ /folder/index.php?id=$1 [NC]RewriteRule ^([A-Za-z0-9_-]+)/page([0-9])\.php /folder/index.php?id=$1&page=$2 [NC]

The RewriteCond was necessary as it also rewrote files in root directory of website. I only wanted to rewrite for the directory 'folder'. Although I am very happy in general there is still one slight issue which I am not sure how to resolve. If someone actually enters the 'folderName/index.php' it brings page not found. I thoughtRewriteRule ^([A-Za-z0-9_-]+)/index\.php /folder/index.php?id=$1&page=$2 [NC]orRewriteRule ^([A-Za-z0-9_-]+)/index\.php /folder/index.php?id=$1&page=1 [NC]would do the trick, but until now I have only crashed the server so far|-) Son

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...