Forum Moderators: phranque

Message Too Old, No Replies

Specify .htaccess Rewrites for Specific Directories

         

Victorious Fize

1:10 pm on Mar 10, 2011 (gmt 0)

10+ Year Member



Alright, here's my story: I spent LITERALLY over 8 consecutive hours trying all sorts of search findings and tutorials to shorten up my MediaWiki URLs, I have become extremely frustrated with this.

For some god forsaken reason, the rules work and the URLs rewrite, but they always hit me with a hard 404, no matter how many edits and messing I do. I swear, I think I'm starting to develop Arithmophobia here.

I'm absolutely clueless when it comes to Apache, so I don't know what causes the rules to conflict, but I am assuming that the fix for this would be to tell Apache that all the rules should be disregarded except for this:
RewriteEngine On
RewriteRule ^/?wiki/(.*)$ /w/index.php?title=$1 [PT,L,QSA]
RewriteRule ^/?wiki/*$ /w/index.php [L,QSA]
RewriteRule ^/*$ /w/index.php [L,QSA]


Right?
Any help would be tremendously appreciated!

Fize

jdMorgan

7:27 pm on Mar 17, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest two things:

First, read the documentation cited in our Forum Charter for necessary information about regular expressions pattern-matching and Apache's mod_rewrite module -- You cannot "guess your way" through either of these aspects of your project, as the syntax of both require *extreme* precision and attention to detail (and so require a firm knowledge base). In many cases, it would actually be faster to stop coding and spend a week(!) studying the documentation than to proceed directly with coding based on guesswork. I'm quite serious, BTW.

Second, it is impossible to reliably determine anything beyond the probable case that your patterns are malformed because we don't know your *intent* in writing or using the rules above. It would be highly useful if you could document your code by adding concise and complete comments to each rule, describing the form of the requested URL that you wish to match, and the filepath to which that URL should be rewritten.

I will however, hazard a guess to illustrate problems with your patterns and use of [QSA], plus a good method of commenting code. If this code is intended for use in your root .htaccess file, then I'd suggest:

RewriteEngine On
#
# Internally rewrite requests for URL-paths in the form /wiki/<post-title> to
# filepath /w/index.php?title=<post-title>, retaining any appended query-string
RewriteRule ^wiki/(.+)$ /w/index.php?title=$1 [QSA,L]
#
# Internally rewrite requests URL-path /wiki/<blank> to filepath
# /w/index.php, retaining any appended query-string by default
RewriteRule ^wiki/$ /w/index.php [L]
#
# Internally rewrite requests for the root URL-path to filepath
# /w/index.php, retaining any appended query-string by default
RewriteRule ^$ /w/index.php [L]

Further, as an SEO consideration, I would strongly suggest replacing the second internal-rewrite rule above with a redirect rule in order to eliminate a duplicate-content problem with "/w/index.php" being directly-accessible by two *different* URLs. However, this redirect must not be implemented until all links to "/wiki/" on your own site have been corrected to link to "/" instead (likely a setting in the wiki configuration file). Once that is done, change the 2nd rule's syntax to "RewriteRule ^wiki/$ [example...] [R=301,L]" and update the comment to refer to a redirect from the old URL-path to the new URL.

Jim