Url Rewriting in PHP Without Htaccess

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.

.htaccess / php url rewriting without routing?

You can create a rewrite rule in .htaccess that routes the movie urls to movie.php as follows:

movie/123:

RewriteRule ^movie/(\d+)$ movie.php?id=$1 [L]

movie/id/123:

RewriteRule ^movie/id/(\d+)$ movie.php?id=$1 [L]

movie/title-of-movie:

RewriteRule ^movie/(\S+)$ movie.php?slug=$1 [L]

movie/title/title-of-movie:

RewriteRule ^movie/title/(\S+)$ movie.php?slug=$1 [L]

combination movie/123/title-of-movie:

RewriteRule ^movie/(\d+)/(\S+)$ movie.php?id=$1&slug=$2 [L]

Edit: added a full .htaccess example for 1 required with up to 2 extra optional parameters with a fallback on index.php if the url is not for movies.

Options -Indexes
IndexIgnore */*

Options FollowSymLinks
AddDefaultCharset utf-8

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteRule ^movie/([^/]+)/?([^/]*)/?([^/]*)$ movie.php?param1=$1¶m2=$2¶m3=$3 [L]

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

^ to match from the beginning

$ to match until the end

? for 0 or 1 occurrence

+ for 1 or more occurrences

* for 0 or more occurrences

If the url rule does not match and the file does not exist then it will route the url to index.php, but you can remove that last part if you don't want that.

How to rewrite URL without .htaccess?

You put the rewrite code in the directory block where you're have the .htaccess file (if you use them). For example if /var/www is your docroot, you could put it their:

    <Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]
</Directory>

Specific url to work with and without .php using .htaccess

You can insert a rewrite rule just below your redirect rule:

RewriteEngine On
RewriteBase /

# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/library/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

# add .php extension to specific URIs
RewriteRule ^(manage)/?$ $1.php [L,NC]

# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L,QSA]

Redirecting Urls via PHP without htaccess

You need to tell your HTTP server that the URL is handled by the PHP script.

You can't do this with PHP directly: If the server never runs the PHP script, then the PHP script can't do anything with the request!

This is most commonly done with mod_rewrite which is configured using Apache configuration. There are two basic places that you can put mod_rewrite directives.

  • The main Apache configuration files
  • A .htaccess file.

The former is recommended:

You should avoid using .htaccess files completely if you have access to httpd main server config file. Using .htaccess files slows down your Apache http server. Any directive that you can include in a .htaccess file is better set in a Directory block, as it will have the same effect with better performance.

… and you've rejected .htaccess so put the directives in your main configuration file.


This, of course, assumes you are using Apache HTTPD. You didn't say which HTTP server you were using.


This also assumes that when you rejected .htaccess you didn't mean mod_rewrite. Many people conflate the two since changes to mod_rewrite settings are the most common things that programmers (as opposed to System Administrators) want to do in an Apache configuration.


For example, Wordpress catches urls like mysite.com/postname and redirects it to other urls

Wordpress uses mod_rewrite directives in a .htaccess file.



Related Topics



Leave a reply



Submit