English 中文(简体)
Mod Rewrite Optional Parameters
原标题:

I have a fairly complex set of rewrite rules to give my site pages pretty URLs. Right now to deal with paging of search results I m using 2 rewrite rules:

RewriteRule ^search/([0-9]+)$ /cgi-bin/search.pl?page=$1 [NC,L,QSA]
RewriteRule ^search$ /cgi-bin/search.pl [NC,L,QSA]

These handle URLs such as:

http://example.com/search
http://example.com/search/2
http://example.com/search/1000

I m wondering how to combine these into 1 rewrite rule so that the search.pl script is called correctly and only passed the page parameter if a page is specified. I know it s a pretty basic question but I can t seem to find the answer anywhere. Thanks for your help!

最佳回答

You could do something like this:

RewriteCond page=$2 ^page=.+|
RewriteRule ^search(/([0-9]+))?$ /cgi-bin/search.pl?%0 [NC,L,QSA]

But that’s not really nicer, is it?


Edit    Or if you’re fine with an empty page value:

RewriteRule ^search(/([0-9]+))?$ /cgi-bin/search.pl?page=$2 [NC,L,QSA]
问题回答

You could use rewritemap with a small external script which maps non-empty to ?page=$1 and empty to empty. I would suggest sticking with what you ve got.





相关问题
Rewrite URL containing spaces

I have an url with the following format: domain/product.php?name=product a Now I need to rewrite this to: domain/product a (including the space in the word) like: http://www.directline-holidays.co.uk/...

mod re-write OS X 10.6

I have OS X 10.6 and am working on a website. When I look at the http.conf file in /private/etc/apache2/httpd.conf it appears that the mod-rewrite module is loaded yet when I add a simple .htaccess ...

Are these two re-writes the same?

How are the following different? Ignore the domain names. RewriteCond %{HTTP_HOST} ^www.example.com$ [NC] RewriteRule ^(.*)$ http://example.com/$1 [R=301,L] RewriteCond %{HTTP_HOST} !^www....

301 Redirect Help with Dynamic URL s and Multiple Id s

I just did a redesign for www.wildchildclothes.com which has a new URL structure. The old site ran on Zen Cart and had a bad URL structure. Here are some examples of what I want to redirect from: OLD:...

Rewrite to index.php best practices

I notice that there are a few common ways to setup RewriteRules for MVC based PHP applications. Most of which contain: RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f RewriteCond %{DOCUMENT_ROOT}...

URL Mod-Rewrite

I currently have a working URL: http://example.com/security-services.php?service=fixed-camera-surveillance and then I have PHP say, $_REQUEST[ service ] to do some stuff... But I d like to ...

Mod Rewrite Optional Parameters

I have a fairly complex set of rewrite rules to give my site pages pretty URLs. Right now to deal with paging of search results I m using 2 rewrite rules: RewriteRule ^search/([0-9]+)$ /cgi-bin/...

热门标签