Hugo Pakula
Hugo Pakula

Reputation: 385

URL Rewrite from base (without prefix) .htaccess

I am developing a short link website for a brand, and I need to have the links look like this:

http://demo-name.com/[shortvalue]

I understand how to do this as following:

http://demo-name.com/l/[shortvalue]

using a basic rewrite rule, however, I am unsure of how to do this without the prefix of the l.

I will also need to be able to go to

http://demo-name.com/file.php

to run certain files, so this needs to be possible.

If it is also possible, I would like to be able to have a

http://demo-name.com/[shortvalue]?key=val&key2=val2
                                 ^^^^^^^^^^^^^^^^^^

query string at the end of my URL's, so if [QSA] is possible as well, that would be great!

Upvotes: 1

Views: 75

Answers (1)

Sumurai8
Sumurai8

Reputation: 20737

You can test if a file is a directory (-d) or a file (-f) in a RewriteCond.

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/\.]+)$ index.php?shorturl=$1 [L,QSA]

This rule will rewrite any "shortvalue" without slashes or dots to index.php?shorturl=shortvalue.

Upvotes: 2

Related Questions