Pretty Urls Without Mod_Rewrite, Without .Htaccess

Pretty URLs without mod_rewrite, without .htaccess

If you've the permissions to set custom error documents for your server you could use this to redirect 404 requests.

E.g. for Apache (http://httpd.apache.org/docs/2.0/mod/core.html#errordocument)

ErrorDocument 404 /index.php

In the index.php you then can proceed your request by using data from the $_SERVER array.

Pretty URLs without mod_rewrite, without .htaccess

If you've the permissions to set custom error documents for your server you could use this to redirect 404 requests.

E.g. for Apache (http://httpd.apache.org/docs/2.0/mod/core.html#errordocument)

ErrorDocument 404 /index.php

In the index.php you then can proceed your request by using data from the $_SERVER array.

URL rewriting in PHP without htaccess

As other people said, just use links like /index.php/nice/looking/url.

The "index.php" in the middle of the URL might look a little strange, but I don't think it's possible to have it look better without .htaccess

Else, you could ask your hoster to redirect any URL to /index.php so that you can handle URL rewriting without having /index.php in your URL.

Then you can just use a regex match to detect what file to include.

preg_match('@[/]{1}([a-zA-Z0-9]+)@', $_SERVER["PATH_INFO"], $matches) ($matches will contain all "parts" of the url in an array)

Be careful with including the files, use a whitelist so you're sure nobody would be able to load internal files.

User friendly URLs without htaccess

Why do you need to show index.php in the URL?

I would create my URL to look like this https://www.sitename.com/register if you truly want clean URL's but you would need to use something like the rewrite.

But you would need to use .htaccess or Apache config rules such as this.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?location=$1 [L]

Then in your PHP code you can do a get on location var $_GET["location"] and then load the page from the value sent.

The result of $_GET["location"] would be register from this URL and then you will display that page.

I don't suggest using MultiViews as it can cause issues if you have file and folders with the same name. e.g. /admin and admin.php.

Clean URLs in PHP without mod_rewrite

As per my knowledge this manipulation is not possible without touching .htaccess.
when you write the url http://www.example.com/videos/name-of-video/ your server is definitely going to look for the index.php file in the folder name-of-video folder. which does not exist.

If this task could have been achieved without .htaccess then they would have never been used.
And by the way why don't you want to use .htaccess..??

How does Wordpress handle all URL formats without .htaccess changes?

The WordPress .htaccess, once in place, requires no changes for new URLs. It reroutes any request for a URL that isn't an existing file or directory to index.php, which then takes the URL and looks into the database for what to display.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

This is known as a front controller and is commonly used in modern web development frameworks.



Related Topics



Leave a reply



Submit