Basj
Basj

Reputation: 46401

htaccess RewriteRule to shorten the URLs

I would like to find a RewriteRule that does this :

mysite.net/jqMAS/ or mysite.net/jqMAS => mysite.net/index.php?id=jqMAS

I used such a .htaccess file :

RewriteEngine On
RewriteRule ^(.*) index.php?id=$1

But unfortunately, it doesn't work (maybe mysite.net/index.php is itself redirected to mysite.net/index.php?index.php, etc. ?) : calling mysite.net/jqMAS produces a 500 Internal Server Error.

What RewriteRule should we use to do such URL shortening ?


Here is what the index.php page (I didn't mention the headers) looks like :

  <body>
  Bonjour <?php echo $_GET['id']; ?>
  </body>

Upvotes: 0

Views: 1587

Answers (3)

nagesh29
nagesh29

Reputation: 56

There are 2 htaccess files: Keep your htaccess file within application folder as: Deny from all.

and paste following code to your htaccess file outside the application folder:

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

It is working perfect for me.

Upvotes: 1

anubhava
anubhava

Reputation: 784968

You need RewriteCond to stop rewriting for real files and directories:

RewriteEngine On

# if request is not for a directory
RewriteCond %{REQUEST_FILENAME} !-d
# if request is not for a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?id=$1 [L,QSA]

Upvotes: 1

Baldvin Th
Baldvin Th

Reputation: 280

Try the following your .htaccess file, as it is the setup successfully used on my own website.

# Enable the rewriting engine.
RewriteEngine On

# Change requests for a shorter URL
# Requires one or more characters to follow the domain name to be redirected
RewriteRule     ^([A-Za-z0-9-]+)/?$     index.php?id=$1             [L] 

Upvotes: 2

Related Questions