]> source.dussan.org Git - nextcloud-server.git/commitdiff
Support pretty URLs
authorLukas Reschke <lukas@owncloud.com>
Wed, 11 Feb 2015 00:10:03 +0000 (01:10 +0100)
committerLukas Reschke <lukas@owncloud.com>
Tue, 1 Dec 2015 15:46:07 +0000 (16:46 +0100)
This changeset allows ownCloud to run with pretty URLs, they will be used if mod_rewrite and mod_env are available. This means basically that the `index.php` in the URL is not shown to the user anymore.

Also the not deprecated functions to generate URLs have been modified to support this behaviour, old functions such as `filePath` will still behave as before for compatibility reasons.

Examples:
http://localhost/owncloud/index.php/s/AIDyKbxiRZWAAjP => http://localhost/owncloud/s/AIDyKbxiRZWAAjP
http://localhost/owncloud/index.php/apps/files/ => http://localhost/owncloud/apps/files/

Due to the way our CSS and JS is structured the .htaccess uses some hacks for the final result but could be worse... And I was just annoyed by all that users crying for the removal of `index.php` ;-)

.htaccess
config/config.sample.php
core/js/config.php
core/js/js.js
lib/private/route/router.php
lib/private/setup.php
lib/private/urlgenerator.php
lib/private/util.php
tests/lib/util.php

index 615f5e7fc968b00b97eecb763b02e29c47900cc4..5c11eff9f372a9f0487c5dfd43b04342343965c7 100644 (file)
--- a/.htaccess
+++ b/.htaccess
   RewriteRule ^remote/(.*) remote.php [QSA,L]
   RewriteRule ^(build|tests|config|lib|3rdparty|templates)/.* - [R=404,L]
   RewriteRule ^(\.|autotest|occ|issue|indie|db_|console).* - [R=404,L]
+
+  <IfModule mod_env.c>
+    SetEnv front_controller_active true
+    RewriteRule ^core/js/oc.js$ index.php [PT,E=PATH_INFO:$1]
+    RewriteRule ^core/preview.png$ index.php [PT,E=PATH_INFO:$1]
+    RewriteCond %{REQUEST_FILENAME} !\.(css|js|svg|gif|png|html|ttf|woff)$
+    RewriteCond %{REQUEST_FILENAME} !/remote.php
+    RewriteCond %{REQUEST_FILENAME} !/public.php
+    RewriteCond %{REQUEST_FILENAME} !/cron.php
+    RewriteCond %{REQUEST_FILENAME} !/status.php
+    RewriteCond %{REQUEST_FILENAME} !/ocs/v1.php
+    RewriteCond %{REQUEST_FILENAME} !/ocs/v2.php
+    RewriteRule .* index.php [PT,E=PATH_INFO:$1]
+  </IfModule>
+
 </IfModule>
 <IfModule mod_mime.c>
   AddType image/svg+xml svg svgz
index 02e5aba3e9460f8d315d6fb073861efa44e1c0f2..034a1ebddbf098c26bdc12663dec3e7ff3d9e115 100644 (file)
@@ -213,7 +213,6 @@ $CONFIG = array(
        )
 ),
 
-
 /**
  * Mail Parameters
  *
index 953bd2ede4550d4b7456ad3ba0a7639ad3b460f3..463e334ef26467aaa1fc2ff3ad589bb6c1521daa 100644 (file)
@@ -141,6 +141,7 @@ $array = array(
                        'version'                       => implode('.', OC_Util::getVersion()),
                        'versionstring'         => OC_Util::getVersionString(),
                        'enable_avatars'        => \OC::$server->getConfig()->getSystemValue('enable_avatars', true),
+                       'modRewriteWorking'     => (getenv('front_controller_active') === 'true'),
                )
        ),
        "oc_appconfig" => json_encode(
index ce552bb8ea2c1a76cbfc5bc8c931a2944fdf2ad2..cbdffd0f016ddfe56edec5f8876ff35af62ecb38 100644 (file)
@@ -160,7 +160,11 @@ var OC={
                        url = '/' + url;
 
                }
-               // TODO save somewhere whether the webserver is able to skip the index.php to have shorter links (e.g. for sharing)
+
+               if(oc_config.modRewriteWorking == true) {
+                       return OC.webroot + _build(url, params);
+               }
+
                return OC.webroot + '/index.php' + _build(url, params);
        },
 
index 6d3b7c742bbc471e0959ce9215c94c3fe497679b..25860ee46bf7bc9d7344b414c095e1e799c55987 100644 (file)
@@ -85,9 +85,15 @@ class Router implements IRouter {
         */
        protected $logger;
 
+       /**
+        * @param ILogger $logger
+        */
        public function __construct(ILogger $logger) {
                $this->logger = $logger;
-               $baseUrl = \OC_Helper::linkTo('', 'index.php');
+               $baseUrl = \OC::$WEBROOT;
+               if(!(getenv('front_controller_active') === 'true')) {
+                       $baseUrl = \OC_Helper::linkTo('', 'index.php');
+               }
                if (!\OC::$CLI) {
                        $method = $_SERVER['REQUEST_METHOD'];
                } else {
index 8f1ae389e45e9bd37eb736a5a2026e3fbd1ec747..1f91240e9da3d7722956ca598046fd064415308b 100644 (file)
@@ -432,6 +432,12 @@ class Setup {
                        //custom 404 error page
                        $content.= "\nErrorDocument 404 ".\OC::$WEBROOT."/core/templates/404.php";
                }
+
+               // Add rewrite base
+               $content.="\n<IfModule mod_rewrite.c>";
+               $content.="\n  RewriteBase ".\OC::$WEBROOT;
+               $content.="\n</IfModule>";
+
                if ($content !== '') {
                        //suppress errors in case we don't have permissions for it
                        @file_put_contents($setupHelper->pathToHtaccess(), $content . "\n", FILE_APPEND);
index 428a222f9c7dae9f8958a75bad13966f746677a9..fb1ea737ef429c335ea10bc232a93d6fbb1cadd2 100644 (file)
@@ -90,7 +90,7 @@ class URLGenerator implements IURLGenerator {
         * Returns a url to the given app and file.
         */
        public function linkTo( $app, $file, $args = array() ) {
-               $frontControllerActive=($this->config->getSystemValue('front_controller_active', 'false') == 'true');
+               $frontControllerActive = (getenv('front_controller_active') === 'true');
 
                if( $app != '' ) {
                        $app_path = \OC_App::getAppPath($app);
index 84a8c49c3839726a3ad1b491ee6ae8f0b2900bf1..5466082d03048f65fe7fa07195ad6d6f0580da61 100644 (file)
@@ -1073,7 +1073,12 @@ class OC_Util {
                                                break;
                                        }
                                }
-                               $location = $urlGenerator->getAbsoluteURL('/index.php/apps/' . $appId . '/');
+
+                               if(getenv('front_controller_active') === 'true') {
+                                       $location = $urlGenerator->getAbsoluteURL('/apps/' . $appId . '/');
+                               } else {
+                                       $location = $urlGenerator->getAbsoluteURL('/index.php/apps/' . $appId . '/');
+                               }
                        }
                }
                return $location;
index 608c5f0c5016288020b597fdce3d8602ecb77190..032ede74a81ae948e2a2c14d4964af3bbcf0fdc4 100644 (file)
@@ -338,6 +338,26 @@ class Test_Util extends \Test\TestCase {
                );
        }
 
+       public function testGetDefaultPageUrlWithRedirectUrlWithoutFrontController() {
+               putenv('front_controller_active=false');
+
+               $_REQUEST['redirect_url'] = 'myRedirectUrl.com';
+               $this->assertSame('http://localhost'.\OC::$WEBROOT.'/myRedirectUrl.com', OC_Util::getDefaultPageUrl());
+       }
+
+       public function testGetDefaultPageUrlWithRedirectUrlRedirectBypassWithoutFrontController() {
+               putenv('front_controller_active=false');
+
+               $_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a';
+               $this->assertSame('http://localhost'.\OC::$WEBROOT.'/index.php/apps/files/', OC_Util::getDefaultPageUrl());
+       }
+
+       public function testGetDefaultPageUrlWithRedirectUrlRedirectBypassWithFrontController() {
+               putenv('front_controller_active=true');
+               $_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a';
+               $this->assertSame('http://localhost'.\OC::$WEBROOT.'/apps/files/', OC_Util::getDefaultPageUrl());
+       }
+
        /**
         * Test needUpgrade() when the core version is increased
         */