homepage Welcome to WebmasterWorld Guest from 54.167.75.155
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
File extension to directory mod_rewrite
philaweb




msg:1501970
 11:20 pm on May 23, 2005 (gmt 0)

Well, I am still fighting my way through understanding the mod_rewrite module. :)

I have this gallery script that turns out dynamic file extentions, like: index?0 , index?1 etc.

Google can't decide whether to keep these pages indexed or to quit them, so they often end up as supplemental results. :(

I wanted to rewrite the file extensions into directories, like this:

index?# to /#

RewriteRule ^(.+[^/])$ /window.php?$1 [L]

But nothing happens!
What am I doing wrong?

 

philaweb




msg:1501971
 11:39 pm on May 23, 2005 (gmt 0)

Actually, the above code gives an error 500 server error. :(

jdMorgan




msg:1501972
 11:46 pm on May 23, 2005 (gmt 0)

> I have this gallery script that turns out dynamic file extentions, like: index?0 , index?1 etc

First you have to modify this script to create the friendly URLs. Then use mod_rewrite to reverse this so that friendly URLs requested from your server are rewritten back to the form that the script can understand; It's a two-step process.

There's nothing wrong with that code snippet. However, you'll have to turn the RewriteEngine on at least once at the top of your mod_rewrite code, and you may need Options +FollowSymLinks in order to do that. There's not enough information in your post to elicit much more help.

Jim

philaweb




msg:1501973
 12:12 am on May 24, 2005 (gmt 0)

jdmorgan

Many thanks for your answer.

My root .htaccess contains the following rewrite code:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ $1/ [R,L]
RewriteRule ^(.+[^/])$ /index.php?$1 [L]
RewriteRule ^(.+[^/])$ /window.php?$1 [L]

The last two lines are rewrite trials. The other lines worked perfectly prior to the addition of the last line. Perhaps I am missing out on something in the context of the code?

By modifying the script code, do you mean the script code should be prepared in some way for rewrite? I was actually considering this, but my insight to PHP is still limited.

Well, on the other hand, it seems that the Wordpress script I also have installed is somehow prepared to handle rewrites, even though I don't know how.

jdMorgan




msg:1501974
 12:19 am on May 24, 2005 (gmt 0)

What is that first rule supposed to do? That looks like it will make your site extremely slow! Not to mention that it may give you 302-redirect-related problems! What was the intent?

You'll need to exclude your script filename from being rewritten in the trial rules. Otherwise, you'll create a loop.

Jim

philaweb




msg:1501975
 12:39 am on May 24, 2005 (gmt 0)

> What is that first rule supposed to do? That looks like it will make your site extremely slow!<

This code is supposed to add a trailing slash, which is does - and very promptly:

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ $1/ [R,L]

It changes the request "www.example.com" to "www.example.com/"

>You'll need to exclude your script filename from being rewritten in the trial rules. Otherwise, you'll create a loop.<

Okay, I get it now... I'm missing a RewriteCond for the trial rewrite rules. :) I have no idea what condition to use.

philaweb




msg:1501976
 1:21 am on May 24, 2005 (gmt 0)

I'm working on it. :)

One line is redundant since I can squeeze them into two lines, one with the conditions and one with the rules.

I will come back after a good nights sleep since it's very late here where I am situated.

jdMorgan




msg:1501977
 1:44 am on May 24, 2005 (gmt 0)

The problem is that it's doing a search of your filesystem for every request that matches the (very non-specific) rewriterule pattern, including all requests for your main script, images, etc.

You might want to try the following, which does the same thing, while doing fewer disk searches:

RewriteCond $1 !/$
RewriteCond $1 !\.
RewriteRule (.+) http://www.example.com/$1/ [R=301,L]

This will not work if you have extensionless files on your site, but it's much faster. The key is to avoid that disk lookup whenever possible. Note also that it's a 301 redirect, to correct slashless search engine listings -- a change you should incorporate in your code, even if the entire snippet won't work on your site. Also, at the very least, exclude you main script from that first rewrite in the same way as shown below.

As for excluding your /window.php file from being rewritten to itself and causing a loop in your trial rule, you could use:

RewriteCond %{REQUEST_URI} !^/window\.php$
RewriteRule ^(.+[^/])$ /window.php?$1 [L]

The procedure for converting to search-engine-friendly URLs is this:

  • Modify the script to produce friendly URLs. Hard-code them or use php preg_replace to modify what's coming out of your database. These friendly URLs are what users will see and click, and what search engines will list and follow.

  • Create rewriterules to modify these incoming URL requests to the form needed to call your script as before; In other words, you rewrite friendly URLs back to unfriendly ones, and invoke your script with those, just as before.

    The key point is that mod_rewrite changes URLs when received from the client (browser, search engine) just before activating the server's content-handler (your script, for example). Mod_rewrite takes effect just after a request is received by your server. It cannot change the URLs on your pages. It's an input function, not an output function.

    Jim

  • philaweb




    msg:1501978
     11:55 am on May 24, 2005 (gmt 0)

    jdmorgan,

    Many thanks for your good advice.

    I have tried the first code and it really does work faster than the previous code, even though it only feels like some nanoseconds. :)

    I have modified the code a bit since I am working with subdomains:

    RewriteCond $1!/$
    RewriteCond $1!\.
    RewriteRule (.+) [[sub1¦sub2¦sub3].example.com...] [R=301,L]

    Now, when I'm testing the trailing slash code, I have noticed that the server default is to skip the "www" subdomain.

    As to the rewrite, I am thinking about having a look into the PHP script before I try further rewrite rules. Whenever I try to rewrite that script output I get server errors.

    Many thanks for your help. I will study your answer more thoroughly when I get the time later. Furthermore, I am going to look into regexp, since I'm going to fiddle with PHP script code anyway. :)

    philaweb




    msg:1501979
     11:59 am on May 24, 2005 (gmt 0)

    Okay, my modification doesn't work. Back to the drawing board. :(

    jdMorgan




    msg:1501980
     2:15 pm on May 24, 2005 (gmt 0)

    You can't have alternates in the substitution string -- With the exception of back-references and server variables, it is a fixed, literal string to be used as the new URL.

    You'll need to reference the current subdomain. Here's one way:

    RewriteRule (.+) http://%{HTTP_HOST}/$1/ [R=301,L]

    Jim

    Global Options:
     top home search open messages active posts  
     

    Home / Forums Index / Code, Content, and Presentation / Apache Web Server
    rss feed

    All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
    Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
    WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
    © Webmaster World 1996-2014 all rights reserved