How to detect if mod_rewrite is available?

时光总嘲笑我的痴心妄想 提交于 2019-12-13 02:24:43

问题


Good day. I have a problem with links in my application. I want to use 'pretty-urls',so i want to all my links look like 'www.sitename.com/controller/method'. So i want to print my all my links through one function,which would look like

function link($url_segments) {
  if(mod_rewrite_is_available) 
     return BASE_URL.$url_segments;
  else
     return BASE_URL."index.php".$url_segments;
}

But i can't understand how to detect if server has rewrite module enabled. I have found 2 solutions:

  1. Place in config some variable(like codeigniter does) like $config["index_variable"] to use inside my function,but that means that all my clients will need to change that line in config when installing application on hosting
  2. Use function to detect mod_rewrite,like apache_get_modules,but like i understood it can be not avaibale on some hosting,and what if server will be nginx?

So i want to know,how to detect that and print my links.

Thanks everyone for help.


回答1:


Can you just provide the .htaccess to your clients so that, when you upload the files, it turns the rewrite mod on?




回答2:


This can be very tricky and hard, personally, I would go with the fallback approach.

  1. Look at apache2handler's loaded modules (via phpinfo()) to see if mod_rewrite is there
  2. Try sudo apache2ctl -t -D DUMP_MODULES to see if mod_rewrite is loaded
  3. Try to use a .htaccess file to see if everything is working.
  4. Try to determine the server software used, and parse the conf file for the server
  5. Use worst case of no mod_rewrite is enabled, and then enable it manually after the software has been installed.


来源:https://stackoverflow.com/questions/8284621/how-to-detect-if-mod-rewrite-is-available

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!