Forum Moderators: phranque
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?
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
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.
You'll need to exclude your script filename from being rewritten in the trial rules. Otherwise, you'll create a loop.
Jim
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.
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]
RewriteCond %{REQUEST_URI} !^/window\.php$
RewriteRule ^(.+[^/])$ /window.php?$1 [L]
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
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. :)