grasshopper
grasshopper

Reputation: 1401

How do you get urls to display with a slash at the end?

There is probably a simple answer for this, but I couldn't find one, so my simpleton question is, how do you get urls to display with a slash at the end?

For example: example.com/page/ instead of example.com/page.html or example.com/page.php

I read http://php.net/manual/en/security.hiding.php but most of it went over my head. I set expose_php to off, but the extension is still there, is this accomplished by editing something in the configuration file or in adding code to a php script?

For reference, I found the mod_rewrite wiki helpful.

Upvotes: 3

Views: 231

Answers (3)

strnk
strnk

Reputation: 2073

This is accomplished by adding URL-rewriting rules to your HTTP server, which will map incoming requests to example.com/page/ to example.com/page.php for example.

Note that using a directory style name (with the trailing slash) can cause some problems when the client will try to access some files related to your page.

  • When fetching example.com/page.php, if the browser encounter, for example, a CSS file to load, he will look for it at example.com/file.css.
  • When fetching example.com/page/ he will try to load example.com/page/file.css, since you were not in the top level folder of the URI anymore. You'll have to add a <base href="http://example.com" /> tag in each of your pages, making the directory style page renaming a bad idea eventually.

Upvotes: 3

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167192

In your PHP code, at the very beginning you can use this:

<?php
    function curPageURL() {
     $pageURL = 'http';
     if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
     $pageURL .= "://";
     if ($_SERVER["SERVER_PORT"] != "80") {
      $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
     } else {
      $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
     }
     return $pageURL;
    }
    if(substr($_SERVER["REQUEST_URI"], -1) != "/")
        header('Location: ' . curPageURL() . '/');
?>

Hope this helps! :)

Upvotes: 0

David
David

Reputation: 4117

you can use mod_rewrite (on a apache server) (it's the better way than the PHP-Parser Variante)

example:

RewriteEngine on 
RewriteRule ^(.*).html$ $1.php

Upvotes: 1

Related Questions