Hello everyone. So I've been dabbling with mod_rewrite and I've run into a snag.

I'm taking www.example.com/index.php?id=news and mapping it to www.example.com/news

Now my problem is that I already have a news directory in my file structure and it's defaulting to the index file found there. What I'm trying to do is allow the user to set the id themselves and I don't want my internal directory to affect their naming.

why not just put it at the top of index.php? I personally don't like using htaccess
www.example.com/index.php
<?php

if(ISSET($_GET['id']) && ctype_alnum($_GET['id'])){
    header("Location: http://www.example.com/{$_GET['id']}");
}

I'm not entirely sure that would work? Basically what I'm trying to do is have htaccess ignore that a directory all ready exists. I want it to redirect id=47 to /news and ignore the contents of the actual physical /news folder. However; I don't want the rule to be too specific in case the situtation changes. Basically is there a way to have htaccess ignore th contents of an existing folder but still remap to it, or will the exisiting file structure always over ride rewrite rules?

right now it looks like this:

            RewriteEngine on

            # Disable rewrite for specific directories 
            RewriteRule ^admin/ - [L] 
            RewriteRule ^admin/index.php - [L] 
            RewriteRule ^admin - [L] 

            #rewrite everything else
            RewriteRule ^([^/\.]+)/?$ index.php?id=$1 [L]

Keep in mind that I'm totally new to this; most of my info on this is what I've been able to pick up through google. Basically the idea is that the admin using the CMS should be able to enter in a name for the id without having to worry about internal folder structures or anything (because they don't even know what they are), I need some sort of fail safe that the page they want is the page they get when they choose these ids, without having to change the folder names on the server (perferably, if possible).

Hmz.. this appears to work here. I'll try to find out what may be possible hickups.

lol okay so it actually work, apparently I am having a noob moment where I didn't bother to check all the paths in the files. All of the paths to the css and everything where relative so inside of the news directory it wasn't finding them >.>

I'm not going to close the thread yet in case I run into more issues :P

This is so strange, it started working out of the blue the extra ?id=news disappeared for no reason I can find (I didn't make any changes to my htaccess file) but suddenly it's back.

So with the current htaccess file above when it gets to the /news directory I get this: /news/?id=news which I assume has something to do with the fact that news is a physical directory and not a virtual one? another problem I'm having, which I know has to do with the fact that the htaccess rules are relative to the current directory (I think) is that instead of getting /aboutus (for example) I'm getting /news/aboutus instead (acutally this might be a problem with the way the urls are generated by the CMS now that I think on it).

Is there some way to write anothe htaccess file for my subdirectories or is there a better way to go about it?

reviving this because I'm back investigating the problem and running into the same issue.

Basically I have two internal directories that all ready exist which I am mapping to with mod_rewrite (the idea is that later am admin with no knowledge of the internal structure can create pages with unique urls and not have the internal directories interfer with their naming - though that does make me realize I will need some sort of check to make sure they don't name it 'admin' or 'images' and overwrite those or something).

I have a media folder and a news folder in my site structure and this is my htaccess file:

            RewriteEngine on

            RewriteBase /
            # Disable rewrite for specific directories 
            #RewriteRule ^admin/ - [L] 
            #RewriteRule ^admin/index.php - [L] 
            #RewriteRule ^admin - [L] 

            RewriteCond %{REQUEST_URI} !^/admin/
            RewriteCond %{REQUEST_URI} !^/admin/index.php
            RewriteCond %{REQUEST_URI} !^/admin
            #rewrite everything else to http://example.com/$1
            RewriteRule ^([^/\.]+)/?$ index.php?id=$1 [L]
            RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?id=$1&newsyear=$2 [L]
            RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?id=$1&newsid=$3&newsyear=$2 [L]

all of my urls work except for /news and /media both of those end up has /news/?id=news and /media/?id=media, the easiest thing to do would have it check if a directory exist when the url is created and ask the user to choose a different url but I am curious as to why this is happening and if there's a work around for it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.