Forum Moderators: phranque

Message Too Old, No Replies

Static URLs with mod rewrite

Need help constructing static URL

         

Produkt

5:56 am on Jan 13, 2010 (gmt 0)

10+ Year Member



Hi, I read the article [webmasterworld.com...] but am having trouble formatting it for my site. I currently have in my .htaccess:

ErrorDocument 401 /error/unauthorized.php
ErrorDocument 404 /error/notfound.php
ErrorDocument 500 /error/server.php

DirectoryIndex home.php index.php index.html

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteEngine off

# Enable mod_rewrite, start rewrite engine
Options +FollowSymLinks
RewriteEngine on
#
# Internally rewrite search engine friendly static URL to dynamic filepath and query
RewriteRule ^photos/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /photos.php?$1=$2&$3=$4 [L]
# Externally redirect client requests for old dynamic URLs to equivalent new static URLs
RewriteCond %{THE_REQUEST} ^[a-z0-9]{3,9}\ /photos\.php\?([^=]+)=([^&]+)&([^=]+)=([^\ ]+)\ HTTP/
RewriteRule ^photos\.php$ http://example.org/photos/%1/%2/%3/%4? [R=301,L]


What I am trying to achieve is example.org/photos/album/1234567890 redirects to example.org/photos.php?album=1234567890 and example.org/photos/album/1234567890/image/1234567890 goes to example.org/photos.php?album=1234567890&image=1234567890. Can someone please tell me what I'm doing wrong? How do I achieve what I intended to do? I appreciate any help anyone may offer.

[edited by: jdMorgan at 4:41 pm (utc) on Jan. 13, 2010]
[edit reason] example.org [/edit]

g1smd

8:33 am on Jan 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The order the rules are listed in is important.

List the External Redirect first.

Add a standard non-www to www canonicalisation rule next.

List your Specific Rewrite next.

List your general (the one with .* in it) rewrite last and add the [L] flag to it.

Make sure that RewriteEngine On appears only once, and that it appears before all of this other code.

Remove the RewriteEngine Off line completely.

Produkt

1:26 pm on Jan 13, 2010 (gmt 0)

10+ Year Member



I have made the changes you recommended, however it still doesn't function as expected. Where are the errors in the following? Thanks again for your help.


Options +FollowSymLinks
RewriteEngine on
# Externally redirect client requests for old dynamic URLs to equivalent new static URLs
RewriteCond %{THE_REQUEST} ^[a-z0-9]{3,9}\ /photos\.php\?([^=]+)=([^&]+)&([^=]+)=([^\ ]+)\ HTTP/
RewriteRule ^photos\.php$ http://www.example.org/photos/%1/%2/%3/%4? [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,L]

# Internally rewrite search engine friendly static URL to dynamic filepath and query
RewriteRule ^photos/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /photos.php?$1=$2&$3=$4 [L]

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

ErrorDocument 401 /error/unauthorized.php
ErrorDocument 404 /error/notfound.php
ErrorDocument 500 /error/server.php

DirectoryIndex home.php index.php index.html

[edited by: jdMorgan at 4:42 pm (utc) on Jan. 13, 2010]
[edit reason] Please use "example.com" or "example.org" only [/edit]

Produkt

5:09 pm on Jan 13, 2010 (gmt 0)

10+ Year Member



I actually believed I've solved the issue. My second RewriteRule now looks like this:

RewriteRule ^photos/([^/]+)/([^/]+)/?([^/]+)?/?([^\ ]+)?/?$ photos.php?$1=$2&$3=$4 [L]

Seems to work now. Thanks for the tips.

jdMorgan

5:26 pm on Jan 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are two rules missing and another that should be added. I think you'll likely be happier with something like this:

ErrorDocument 401 /error/unauthorized.php
ErrorDocument 404 /error/notfound.php
ErrorDocument 500 /error/server.php
#
DirectoryIndex home.php index.php index.html
#
Options +FollowSymLinks
RewriteEngine on
#
# Externally redirect client requests for old dynamic URL-paths (/photos.php?album=1234567890&image=
# 1234567890) to equivalent new static URLs (example.org/photos/album/1234567890/image/1234567890)
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /photos\.php\?album=([^&]+)&image=([^&#\ ]+)\ HTTP/
RewriteRule ^photos\.php$ http://www.example.org/photos/album/%1/image/%2? [R=301,L]
#
# Externally redirect client requests for old dynamic URL-paths (/photos.php?album=1234567890)
# to equivalent new static URLs ( example.org/photos/album/1234567890
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /photos\.php\?album=([^&#\ ]+)\ HTTP/
RewriteRule ^photos\.php$ http://www.example.org/photos/album/%1? [R=301,L]
#
# Externally redirect direct client requests for index page
# filepaths to "/" URLs, preserving directory & query string
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*((index¦home)\.php¦home\.html)([?#][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)((index¦home)\.php¦home\.html)$ http://www.example.org/photos/album/$1 [R=301,L]
#
# Externally redirect to remove "www"
RewriteCond %{HTTP_HOST} ^www\.([^.:/]+(\.[^.:/]+)+)\.?(:[0-9]+)?$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#
# Internally rewrite search engine friendly static URL-path (/photos/album/1234567890/image/1234567890)
# to dynamic filepath and query (/photos.php?album=1234567890&image=1234567890)
RewriteRule ^photos/album/([^/]+)/image/([^/]+)$ photos.php?album=$1&image=$2 [L]
#
# Internally rewrite search engine friendly static URL-path (/photos/album/1234567890)
# to dynamic filepath and query (/photos.php?album=1234567890)
RewriteRule ^photos/album/([^/]+)$ photos.php?album=$1 [L]
#
# Rewrite remaining requests to .php script if requested URL-path does not already end with a filetype and
# does not resolve to an existing directory, but does resolve to existing file if ".php" is appended.
RewriteCond $1 !\.[a-z0-9]+$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

Note that this code is not tested, and due to insufficient caffeination levels, may contain typos or other errors. :)

The exclusion of requests with appended filetypes in the last rule above is likely to have a good positive effect on the speed of your site. You may wish to make the pattern more-selective, but I suggest that you do not simply remove that RewriteCond.

Important: Replace all broken pipe "¦" characters above with solid pipe characters before use; Posting on this forum modifies the pipe characters.

Jim

mackit

12:00 am on Apr 2, 2010 (gmt 0)

10+ Year Member



Jim, I've followed your post as a guide for how to redirect my old dynamic urls to my new static ones, but I can't get it to work. I have all the other rules working to rewrite my static urls to their proper dynamic locations, I just can't get the RewriteCond part to work. Do you know what I'm doing wrong? Here's my code:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php\?catalog=([^&]+)&page=([^&#\ ]+)\ HTTP/ 
RewriteRule ^index\.php$ http://www.example.com/catalog-test/%1/%2/? [R=301,L]

This rule is in an .htaccess file within a subdirectory of the webserver root, namely "/catalog-test", where the script "index.php" also resides. I want to take a url like this:
www.example.com/catalog-test/index.php?catalog=2010&page=5
and redirect it to this:
www.example.com/catalog-test/2010/5

I can't figure out why it's not working. I have this code before all the other rewrite rules.

jdMorgan

1:29 am on Apr 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Neither can I... Your code looks fine for the purpose and location that you describe.

Did you delete your browser cache before trying to test it?

Jim

jdMorgan

12:56 pm on Apr 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I missed the implication of this:

> This rule is in an .htaccess file within a subdirectory of the webserver root, namely "/catalog-test", where the script "index.php" also resides.

So if "/catalog-test" appears in the URL for these requests, you'd need:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[i]catalog-test/[/i]index\.php\?catalog=([^&]+)&page=([^&#\ ]+)\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/catalog-test/%1/%2/? [R=301,L]

because THE_REQUEST is the HTTP request line as sent by the browser, exactly as it appears in your raw server access log file.

Jim

mackit

6:30 pm on Apr 2, 2010 (gmt 0)

10+ Year Member



Okay, I've gotten it to go to the correct page now, but it's still not externally redirecting the URL. The page shows up as it should, but the URL does not change in the path bar. (Also, I had to un-escape the question mark before "catalog=...") Is it possible that my internal rewrites could be interfering with my external ones?

[edit]
Hmm, it's not the internal rewrite rules, because I tried removing all of the rule except the one I posted, and the dynamic URL still accesses the page.

g1smd

6:41 pm on Apr 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Did you flush the browser cache before testing again?

The question mark should be escaped. It is a literal question mark in THE_REQUEST.

By unescaping it, you're turning it into a test of whether the request contains
index.php
or
index.ph
here.

mackit

9:49 pm on Apr 5, 2010 (gmt 0)

10+ Year Member



Refresh Cache...Doh! That was it. I'm now forcing myself into the habit of doing that each time I make changes to an .htaccess file. Thanks for the help, guys