TIP 4: use minimal options<\/span><\/h3>\nWhen nothing is working, it\u2019s tempting to just start sticking various options to RewriteRule to see what happens.<\/p>\n
The options that you can pass to RewriteRule are a bit cyptic. They are:<\/p>\n
[L] = Last rule. If there is a match then stop processing further Rewrite rules. \n[QSA] = Query String Append. Useful for scripts that receive parameters via a GET (foo.php?id=123&name=joe) and you simply want to pass them on. \n[PT] = Pass through. Mostly used in combination wiAfter a match, pass through to the other handlers. U \n[R] = Redirect. Send the rewritten URL back to the browser, which the browser automatically loads. The new URL is visible to end-user.<\/p>\n
The [L]ast rule is a nice optimization if you have several rules, since the server won\u2019t continue to match rules that you don\u2019t need or want. Having rules that call other rules seems like a recipe for spaghetti code. For example, this is possible:<\/p>\n
RewriteEngine On \nRewriteRule ^xxx$ yyy \nRewriteRule ^yyy$ zzz \nRewriteRule ^zzz$ news.php \n<\/code><\/p>\n\u201cxxx\u201d, \u201cyyy\u201d or \u201czzz\u201d would all take you to \u201cnews.php\u201d. Useful? Maybe. It\u2019s certainly convoluted.<\/p>\n
Here\u2019s how you add an options to your rule.<\/p>\n
RewriteEngine On \nRewriteRule ^latest$\t\t\t\t\tnews.php [L] \n<\/code><\/p>\n[QSA] is only useful if you have a script that receives input multiple variables via a GET request. Assuming that news.php takes several parameters\u2026<\/p>\n
RewriteEngine On \nRewriteRule ^latest$\t\t\t\t\tnews.php [QSA,L] \n<\/code><\/p>\n[PT] is only useful if you want mod_alias to process the URL afterwards. (But why not just write the URL correctly the first time?)<\/p>\n
TIP 5: Use simple patterns<\/span><\/h3>\nThe regexp operator \\d<\/code> which normally matches all digits, doesn\u2019t work! I figured this was a fairly basic to any regular expression engine, but it must be a perl extension. Anyways, keep it simple. If your regexp doesn\u2019t work, simplify it to the basics.<\/p>\nURL: \n\/\/site.com\/_item.php?id=123<\/p>\n
DESIRED URL: \n\/\/site.com\/item\/123<\/p>\n
# Bad: Does NOT work! \nRewriteRule ^item\/(d+) _item.php?id=$1<\/code><\/p>\n# Good \nRewriteRule ^item\/([0-9]+) _item.php?id=$1<\/p>\n","protected":false},"excerpt":{"rendered":"
\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435TIP #1: Check that .htaccess is being read and mod_rewrite is availableTIP 2: Redirects must have different names than files and directoriesTIP 3: The URL your pattern sees is relative to the current directoryTIP 4: use minimal optionsTIP 5: Use simple patterns \u0418\u0441\u0442\u043e\u0447\u043d\u0438\u043a \/\/www.xinsight.ca\/blog\/tips-for-configuring-apaches-mod_rewrite\/ Nice URLs are good interface design. But, it can be a […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[695],"tags":[],"yoast_head":"\n
Tips for configuring Apache\u2019s mod_rewrite<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n