Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess rewrite rules

Tags:

.htaccess

I'm trying to get the following effect:

  1. A user visits example.com/profile/Tom and it, in the background, makes it example.com/profile.php?username=Tom
  2. Instead of making line after line of .htaccess for each URL (Figure 1), I want it to be short and work nicely, e.g. the user visits example.com/about and it uses about.php, then example.com/helloworld and it uses helloworld.php

As right now, I have to put the following for each URL I want, and instead I want 2-3 lines of .htaccess that would do the job. (If possible, more is fine, I'm not great with .htaccess)

RewriteRule ^login/submit$ login.php?action=submit
like image 317
oyed Avatar asked May 19 '26 05:05

oyed


1 Answers

Try this and let me know.

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^profile/([^/])/? profile.php?username=$1 [L,NC]

RewriteRule ^login/submit$ login.php?action=submit [L,NC]

# /login/ will go to login.php or /contact/ will go to contact.php
RewriteRule ^([a-z0-9_\-]+)/? $1.php [L,NC]

Here's the full documentation about the [] flags: http://httpd.apache.org/docs/2.3/rewrite/flags.html

like image 182
Book Of Zeus Avatar answered May 21 '26 03:05

Book Of Zeus