Rewriting all but one special case

Discuss practical ways rearrange URLs using mod_rewrite.

Rewriting all but one special case

Postby Jakeis » Sun Jun 21, 2009 8:23 pm

Hi,

I've recently added a pagination system to my tag listings that limits database results to a threshold of N records on a page. For example:

tag-search/index.php?tag=mytag&page=1

Using mod_rewrite rules, I've given each tag listing page a friendly URL, where the page number is optional (if omitted, page=1 is assumed)...

example.com/tag/mytag
example.com/tag/mytag/1

My problem is trying to find a mod_rewrite rule that will work with my current rules to drop /1 from any URL since it is unnecessary, and for SEO purposes.

My current rules are as follows:

Code: Select all
RewriteRule      tag/(.+)\/(.+)      tag-search/index.php?tag=$1&page=$2   [NC,L]
RewriteRule      tag/(.+)            tag-search/index.php?tag=$1         [NC,L]


Thank you!
Jakeis
 
Posts: 8
Joined: Fri Mar 13, 2009 12:26 am

Postby richardk » Mon Jun 22, 2009 7:48 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^(tag/[^/]+)/1$        /$1 [R=301,L]
RewriteRule ^tag/([^/]+)/([0-9]+)$ /tag-search/index.php?tag=$1&page=$2   [NC,L]
RewriteRule ^tag/([^/]+)$          /tag-search/index.php?tag=$1&page=1    [NC,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Jakeis » Mon Jun 22, 2009 8:42 am

Yes! Perfect.
You're the best, Richard! Thank you kindly!
Jakeis
 
Posts: 8
Joined: Fri Mar 13, 2009 12:26 am

Postby Jakeis » Sat Jun 27, 2009 3:08 pm

One more question about this issue, Richardk:

How can I add a rule to change the address bar URL to the friendly form?

Example:
Any URL entered directly as:
/tag-search/index.php?tag=myTag&page=1

Will show in the address bar as:
/tag/myTag

...even if under the Apache hood the original form is what's being used?

Thanks again.
Jakeis
 
Posts: 8
Joined: Fri Mar 13, 2009 12:26 am

Postby richardk » Sun Jun 28, 2009 12:40 pm

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Don't match the internal mod_rewrite sub requests.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# Match the tag name (in %2).
RewriteCond %{QUERY_STRING} ^(.*&)?tag=([^&]+)(&.*)?$ [NC]
# Don't match the page number if it's page=1. (The tag name is now in %1.)
RewriteCond %2&%{QUERY_STRING} ^([^&]+)&(.*&)?page=1(?:&.*)?$ [NC,OR]
# Or match the page number if page > 1 (in %3). (The tag name is now in %1 with a trailing slash.)
RewriteCond %2/&%{QUERY_STRING} ^([^&]+)&(.*&)?page=([0-9]+)(&.*)?$ [NC]
# Match /tag-search and redirect.
RewriteRule ^tag-search/(index\.php)?$ /tag/%1%3? [R=301,L]

RewriteRule ^(tag/[^/]+)/1$        /$1 [R=301,L]
RewriteRule ^tag/([^/]+)/([0-9]+)$ /tag-search/index.php?tag=$1&page=$2   [NC,L]
RewriteRule ^tag/([^/]+)$          /tag-search/index.php?tag=$1&page=1    [NC,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Jakeis » Sun Jun 28, 2009 1:11 pm

That's quite an impressive piece of mod_rewrite code. And it works, partially.

URLs of the form:
/tag-search/index.php?tag=download&page=[0-9]
get redirected to the friendly version

However, if the page variable is left off, there is no redirection:
/tag-search/index.php?tag=download

Also, if I add another query string variable down the road, will this be difficult to modify? I'm thinking of adding a "sort=rating" to optionally sort the tag results by each record's rating.

Thanks again, much appreciate the help with this.
Jakeis
 
Posts: 8
Joined: Fri Mar 13, 2009 12:26 am

Postby richardk » Mon Jun 29, 2009 11:15 am

However, if the page variable is left off, there is no redirection:
/tag-search/index.php?tag=download

Replace
Code: Select all
RewriteCond %2/&%{QUERY_STRING} ^([^&]+)&(.*&)?page=([0-9]+)(&.*)?$ [NC]

with
Code: Select all
RewriteCond %2/&%{QUERY_STRING} ^([^&]+)&(.*&)?page=([0-9]+)(&.*)?$ [NC,OR]
# or just match the tag name
RewriteCond %2 ^(.+)$


Also, if I add another query string variable down the road, will this be difficult to modify? I'm thinking of adding a "sort=rating" to optionally sort the tag results by each record's rating.

To add it to the rewrite (the last three rules) it would not be a problem.

For the redirect it's not too complicated, but you have to pass it on like the tag name and that gets messy the more variables you add. You can also do this type of redirect with PHP, if you have lots of variables or you want to do complicated things.

But if you haven't already used the query string with the "sort=rating" variable you don't really need to set up a redirect.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Jakeis » Mon Jun 29, 2009 1:12 pm

Great, thanks! It works just the way I want it to.

Much appreciated!
Jakeis
 
Posts: 8
Joined: Fri Mar 13, 2009 12:26 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 23 guests

cron