Recursive mod_rewrite for search engine friendly urls

前端 未结 4 980
别那么骄傲
别那么骄傲 2020-12-30 18:11

I\'ve been reading through a previous solution to a recursive mod_rewrite problem that is similar to what I\'m trying to do, the difference is I\'m sending all queries throu

相关标签:
4条回答
  • 2020-12-30 18:25

    I understand this is a very old thread. But since none of the answers posted here worked for me and I want to post my answer so that visitors can use it if they want so I am answering it here:

    Options +FollowSymlinks -MultiViews
    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/([^/]+)(/.*)?$ $3?$1=$2 [N,QSA,DPI]
    RewriteRule ^(/[^/]+|[^/]+/|/?)$ /index.php [L,QSA,DPI]
    

    For more details please see my answer on MOD_REWRITE for URL variables, removing empty parameters and using more than 1field for the first and second variables

    0 讨论(0)
  • 2020-12-30 18:34

    I copied the solution from that other question and modified it like this:

    RewriteCond %{QUERY_STRING} ^(.*)$
    RewriteRule ^(.*/)?([^/]+)/([^/]+) $1?$2=$3&%1 [L]
    RewriteCond %{QUERY_STRING} ^(.*)$
    RewriteRule ^.*$ index.php?%1 [L]
    

    It does nearly the same thing, except in the first rule, the first match is optional and in the second rule, the match is on whatever is left after all the other pairs are matched.

    For an odd number of parameters, the first parameter is ignored.

    One note, if you expect to have a lot of parameters, you may have to change some settings.

    Add something like this to your .htaccess file

    RewriteOptions MaxRedirects=20
    

    and something like this to your apache conf file

    LimitInternalRecursion 20
    

    Instead of "20" pick whatever number of recursions (pairs) you need to allow (the default is 10).

    0 讨论(0)
  • 2020-12-30 18:37

    Try these rules:

    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]
    RewriteRule ^([^/]+)/([^/]+)(/(.*))?$ /$4?$1=$2 [N,QSA]
    RewriteRule ^$ index.php [L]
    

    The first rule is to exclude requests for existing files. The second rule will do the work and rewrite each name and value pair. And the third rule is to rewrite the final URI to index.php.

    0 讨论(0)
  • 2020-12-30 18:41

    yes...

    Taken from the examples here:

    http://iirf.codeplex.com/sourcecontrol/changeset/view/62027?projectName=IIRF#981491

    This ruleset iteratively translates a pair of URL path segments to a querystring n=v segment.

    # handle case with no query string.  This rule fires the first time through,
    # and converts the first pair of URL path segments to a n=v query string segment.  
    RewriteRule ^/(?!index\.php)([^\?\/]+)/([^\?\/]+)/([^\?]+)$ /$3?$1=$2
    
    # handle the case where there is an existing query string, and more than one pair of 
    # URL path segments remaining. This rule fires potentially multiple times.
    RewriteRule ^/(?!index\.php)([^\?\/]+)/([^\?\/]+)/([^\?]+)\?(.+)$ /$3?$4&$1=$2
    
    
    # Handle the case with a query string, and exactly one pair of URL path segments. 
    # This fires once (last).
    # It fires when there is an even number of segments. 
    RewriteRule ^/(?!index\.php)([^\?\/]+)/([^\?\/]+)\?([^\?]+)$ /help.cfm?$3&$1=$2  [L]
    
    # Handle the case with no query string, and exactly one pair of URL path segments. 
    # This fires once (last).
    RewriteRule ^/(?!index\.php)([^\?\/]+)/([^\?\/]+)$ /help.cfm?$1=$2  [L]
    
    
    # Handle the edge case, where there is an odd number of segments, which is invalid
    # for these purposes. 
    #
    # This fires once, after all the other pairs have been parsed.  In fact the filter
    # cannot know that there is an odd number of segments until it does all the matching.
    # So at the end we see, we have one left over segment, and we 
    # redirect to a 404 page.
    
    RewriteRule ^/(?!index\.php)([^\/\?]+)\?([^\?]+)$ /ResourceNotFound.php  [L]
    
    # Handle the edge case where there is only one segment. This is also an error
    # in this ruleset.
    RewriteRule ^/(?!index\.php)([^\/\?]+)$ /FoundOnlyOneSegment.php  [L]
    

    This ruleset might not be exactly what you want but it illustrates the approach. It was not developed for mod_rewrite, but for IIRF, which is a rewriter for IIS. But the syntax should be the same for mod_rewrite, so these rules should just work.

    I don't know if mod_rewrite has a logging facility, or a command-line test capability, but IIRF does, which makes it easier to see how individual rules work, or the outcome of sets of rules.

    IIRF has an iteration limit that defaults to 10. There's a way to raise that limit to 30, which is pretty high, but still not infinite. That means it won't work for "any number of parameters". It can convert up to 15 pairs of params. I don't know if mod_rewrite has a similar limit.

    0 讨论(0)
提交回复
热议问题