views:

85

answers:

1

Hey guys, I'm trying to implement one of the answers I got to another question I asked on here a couple days ago. You can find the original question here: http://stackoverflow.com/questions/1515639/modrewrite-clarification-question-only-for-dynamic-urls

The most helpful answer and the one I'm modeling the implementation after is as follows:

I'm guessing the answer meder gave is the one you're looking for but technically you can create a static map file to redirect a set of title strings to ids and it doesn't have to execute an external prg executable:

  RewriteMap static-title-to-id txt:/tmp/title_to_id.txt
  RewriteRule ^/health-and-fitness-tips/(.*)/ /health-and-fitness-tips/${static-title-to-id:$1}/ [L]

with the contents of the /tmp/title_to_id.txt file something like this:

  how-do-I-lose-10kg-in-12-weeks 999
  some-other-title 988
  and-another 983

Ok enough background. My .htaccess file currently has the following in it:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Its a typical wordpress permalink customization. However, when I try to add rules similar to those presented in the selected answer above, I get an internal server error.

This is my .htaccess file when I get the internal server error:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteMap static-title-to-id txt:/tmp/title_to_id.txt
RewriteRule ^/health-and-fitness-tips/(.*)/ /health-and-fitness-tips/${static-title-to-id:$1}/ [L]
</IfModule>

I'm hoping my problem is something simple, like a rule conflict. If you can see an error or can provide some direction here, it would be much appreciated.

+1  A: 

There are four errors in your code:

  1. The RewriteMap direction can only be used in the server configuration or virtual host context.

  2. When using mod_rewrite in an .htaccess file, mod_rewrites first removes the per-directory prefix from the URL path before testing the rules and reappended after applying a rule. In the case the .htaccess is in the root directory, the path prefix would be / that’s removed. So you need to specify the path pattern without that prefix:

    RewriteRule ^health-and-fitness-tips/(.*)/ /health-and-fitness-tips/${static-title-to-id:$1}/ [L]
    
  3. The substitution of your rule would also be matched by that same rule. That would lead to an infinite loop. So you need to either change the pattern or the substitution to avoid that. In your case changing the pattern would be better, for example:

    RewriteRule ^health-and-fitness-tips/([a-zA-Z]+[a-zA-Z0-9]+|[a-zA-Z0-9]+[a-zA-Z]+)/ /health-and-fitness-tips/${static-title-to-id:$1}/ [L]
    

    Your should also specify the end of the URL path:

    RewriteRule ^health-and-fitness-tips/([a-zA-Z]+[a-zA-Z0-9]+|[a-zA-Z0-9]+[a-zA-Z]+)/$ /health-and-fitness-tips/${static-title-to-id:$1}/ [L]
    
  4. As I assume that those URLs can not be directly mapped to existing files or directories, your first rule will catch them before you rewrite them. So you should change the order of that rules to have the specific rule applied before the catch-all one:

    RewriteRule ^health-and-fitness-tips/([a-zA-Z]+[a-zA-Z0-9]+|[a-zA-Z0-9]+[a-zA-Z]+)/$ /health-and-fitness-tips/${static-title-to-id:$1}/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    
Gumbo
Thanks for your answer Gumbo! With the RewriteMap, I just need to add it to the httpd.conf file and the {static-title-to-id:$1} should work? Or do I have to call the httpd.conf file in some way?
ivannovak
@ivannovak: No, just add it to your httpd.conf file and restart Apache since the httpd.conf file is only read once at the start of Apache.
Gumbo
@Gumbo: My .txt map file is generated by a php script. How should I automate the detection of new map rules each time a new post is added? Or is this a food for another question?
ivannovak