开发者

writing .htaccess rules

I already have the site with parameters in the url. now i am thinking to do the clean url for the users to avoid the parameters in the url. I studied about the .htaccess rules(mod rewrite) but one thing i don't understand is, do i need to change the url's in all the places so that rules in htaccess will work? I feel this is more tedious as I need to find the places and change it to clear url. For example, I have http:开发者_运维百科//www.mysite.com/index.php?view=about and http://www.mysite.com/index.php?view=contact. I wrote the rule as

RewriteEngine On
RewriteRule ^([^/]*)\.html$ /?view=$1 [L]

this works fine if i go and type mystie.com/about.html OR mysite.com/contact.html. But the thing is now I need to find the place in the coding and change all view=about or view=contact to about.html or contact.html. In this case it might be simple as it will be just the header i need to change..but if there are url's like view=showdata?user=dsdsa&aga=12...i cannot find all the places where this is mentioned and change. any easy way or smart approach? thanks for your help.


You would have to re-engineer your code.

However, there are chances to do the remap programmatically at cost of performance.

Since you run on PHP, you can use the ob functions of output buffering to:

  1. Buffer the output on startup
  2. Retrieve the buffer contents, and clear it, once you rendered all the page
  3. Apply a regex to convert URLs
  4. Write the modified buffer to output stream

You obviously need a set of rules to be mapped in a PHP script. Sorry for providing you with no code but I did this years ago, basing on other code I found. But the approach is straightforward.


The rewrite rule will only affect URLs that end in .html. Therefore, your links directly to index.php should remain fully working. Those ugly links, though, would be picked up by search engines and appear in results pages.

Under your current rewrite rule, URLs that include parameters like user=dsdsa cannot be rewritten as clean URLs (e.g. as /showdata.html?user=dsdsa). This is because if you include a query string in the rewrite rule's replacement substring, Apache will delete the other parameters. You can avoid this by using the [QSA] flag. See Apache documentation for more details.

I cannot say how to find all the ugly links in the PHP code. You might have to resort to looking at each place where view= exists and manually updating it.


If I understand your question, you are asking if you need to change all the hardcoded URL's that you have in your code.

The answer in this case is no, you don't need to change the URL's, the advantage of using mod_rewrite when changing URL's is that the old URL's remain active. It doesn't mind if you type the URL directly in the browser, of you click a link in a page, they generate the same HTTP request (with the exception of the referrer).

Of course, you could change your code gradually so it uses the real URL's, in order to avoid confusion.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜