Pretty Url Problems

Hi everyone,

I’m having some trouble implimenting pretty url’s on my site, this code worked before but i changed my sites folder hierarchy, but when i changed the code to match the new structure it broke.

What i would like is that some one could type in the pretty URL and on the server side it would be converted into the proper URL, as shown below.

Here is what needs to be done:

Orginal URL:

Formatted Pretty URL:

Here’s code:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^author/(.*)$ ./author.php?name=$1

Any help would be appreciated.

Thanks,
Maxdream01

Oh sorry did not realize that there is a specific place for this, please move to Server Configuration, Apache & URL Rewriting forum. :o

Thanks

I am 86% certain on this, but I think the issue is with your ./author.php?name=$1

I think the ./ is telling apache to look in the folder author/ for author.php which doesn’t exist. If you change it to the following, it might work

Options +FollowSymLinks
RewriteEngine On  
RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f  

RewriteRule ^author/(.*)$ author.php?name=$1

@dklynn ; would also suggest NOT using (.*), and instead only capture the characters you want. Such as ([a-zA-Z0-9-]+), which captures letters (upper and lowercase), numbers, and the hypen (-).

cp,

You must’ve been “abused” by me as you’re doing quite well with your mod_rewrite responses! :tup:

md1,

I believe that your problem (aside from the obvious (.*))was with the redirection. I must assume that your .htaccess is in your DocumentRoot so the redirection will be relative to that. The problem is that you do not need to specify the current directory with the ./, all you need is author.php.

Tip: I try to put all my mod_rewrite code in the DocumentRoot’s .htaccess so I don’t have to consider directory levels (except relative to the DocumentRoot, of course). A second tip (a twofer, how about that!) is to use the R=301 flag (along with the Last flag) so you can see the redirection you’ve made. At least that would have shown you something really weird and would have forced you to look at the redirection again and you may have noticed the level change.

WARNING: When you change the directory level, you will create a directory offset for any embedded relative links resulting in the loss of css, js and image links (relative to author.php’s location in the DocumentRoot). I’ll let you read my signature’s tutorial to discover the two options to resolve this problem.

Regards,

DK

The nightmares were terrible, I finally got over them until a few threads ago :wink: No, I really glad that I’ve run into you in regards to mod_rewrite, I’ve learned a lot (still have more to learn, but that’s why I try).

cp,

:rofl: Whew!

As for mod_rewrite, all it takes is a little experience (and answering member questions for too many years).

Regards,

DK

Hey everyone, thanks for the help so far, but its still not working. Instead i’m getting a weird anomaly where my browser is trying to go to www.site.com/author/some-authors-name/index.php.

here is the code i’m now using, i changed it accordingly:


Options +FollowSymLinks
RewriteEngine On 
RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f 
RewriteRule ^author/([a-zA-Z0-9-]+)$ author.php?name=$1

Does it work if you change

Options +FollowSymlinks

to

Options +FollowSymlinks -MultiViews

?

md1,

MultiViews allows a URI to be hijacked if there is an identical filename in the path, i.e., author.php and author/ where author.php will actually be run. I ensure that it’s off (-MultiViews) as you can get really weird results if you’re not careful.

You neither showed us your test URI nor the result of an R=301 flag (used for testing, it will display the redirection) but that’s about the only thing that could have hijacked your URI UNLESS you have other mod_rewrite (or mod_alias) code in your .htaccess (which should also be displayed for reasonable help).

Regards,

DK

Thank you! I was playing around with this last night and got weird results using /author/testing becuase I had an author.php, and I eventually found that renaming the file to author2.php worked, and so did using -MultiViews, but I didn’t have time to research why it worked yet. Your explanation even helped me :slight_smile:

cp,

Thanks! I think SitePoint should bring you on staff! Oh, wait, they have :rofl: You’re doing very well so keep learning (and visit apache.org for the straight skinny on their modules and core directives - I do all the time!).

Regards,

DK

Hi everyone, I tried placing -MultiViews in the code and it stopped the the server from trying to go to /author/author-name/index.php, but now I’m receiving a 300 multiple options error, trying to make me go to author.php/author-name/.

I do not have any other mod_rewrite commands in .htaccess.

Thanks for the help,
Maxdream01

I had to change the rewrite rule to the following to support hypens and a possible ending slash

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^author/([-a-zA-Z0-9]+)/?$ author.php?name=$1

Hi cp,

I tried that code and for some reason i’m only receiving a 404 error.

Thanks,
Maxdream01

What URL were you testing? I did get a 404 for testing /author/author-name/index.php, but it works for /author/author-name/ and /author/author-name

I recieved a 404 for both /author/author-name and /author/author-name/

md1,

There is no reason for those URIs not to work (except for the comment below about trailing /'s). Add the flags I added to cp’s code (after confirming that you have author.php in your DocumentRoot (the same directory as this .htaccess).

cp,

Optional trailing will (almost) always kill relative links in your scripts so, unless you’re willing to code absolute links or add the <base> meta tag to the author.php script, it’s best not to allow optional /'s! In addition, the {REQUEST_FILENAME} is the variable which is commonly used to test files and directories. I think that {SCRIPT_FILENAME} does give the same answer but I’d have to go look it up and I’m swamped for time right now.

both,

The flags can be very useful! In this case, the R=301 flag will tell Apache to display the redirection (to help discover the reason for the 404) and can be removed before the code goes to the production machine. Obviously, the Last flag tells mod_rewrite to restart the next pass through the code.

Regards,

DK

Hi Dk,

This change gave me very strange results, if i try to go to /author/author-name/ it tries to take me to /author.php/author-name/index.php, which does not exist, and if i try to type in /author/author-name it tries to take me to the full server root, e.g. [I]http://www.site.com/******/homepages/*/********/htdocs/rfid/author.php?name=ari-boyarsky[/I], this is the actual server root where the files are located, its a shared server, the * represent information I can’t disclose here, but its just a string of numbers that identifies each site on the server.

Thanks,
Maxdream01

md1,

Now we’re getting somewhere!

Okay, author/author-name/ identifies a directory and DirectoryIndex is obviously set for index.php {others? none were found} so it gave you author/author-name/index.php as it is supposed to do (I trust that you removed the optional trailing / for the reasons above).

Now, the server root is a problem when /author.php was not requested (when it doesn’t find author.php in the server root, it’s supposed to look in the domain’s DocumentRoot, which it has done, but it’s supposed to relative to the DocumentRoot, not the server root). I’ve had that same problem (on my WinDoze test machine). Many others have had that problem, too, and the only way I/we have found to resolve that problem is to restart Apache. Give that a try as it should restore Apache to full (and correct) functioning.

Regards,

DK

DK,

I don’t believe i have the power to restart apache, firstly the web host the client is using is 1and1, which uses cgi not apache, and even if it did use apache i do not believe i would have the power to restart apache anyway because it is a shared server.

Thnaks,
Maxdream01