问题
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:
- 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 - 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.
- Look at
apache2handler
's loaded modules (via phpinfo()) to see ifmod_rewrite
is there - Try
sudo apache2ctl -t -D DUMP_MODULES
to see if mod_rewrite is loaded - Try to use a
.htaccess
file to see if everything is working. - Try to determine the server software used, and parse the conf file for the server
- 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