EasyBB
EasyBB

Reputation: 6574

Rewrite rule for clean url

I'm trying to write a clean url for profiles on my website. For example I'd want this url

/profile?user=MrEasyBB

to become

/MrEasyBB

My current htaccess starts like this

Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ profile.php?user=$1 [NC,L]

DirectoryIndex index.php

Which this should all work if I am correct. But what happens per say if the page url they type is

/help

There shouldn't be a user named help, and the page should direct to help.php instead. Can I write a rule to not follow a certain pattern? I'd also like to know to change the rule for images, comments, and so forth:

IE

/image/user/MrEasyBB/1239123871712194124.jpg

instead of

/image/picture.php?user=MrEasyBB&image=1239123871712194124.jpg

Which I am not positive on how to add multiples and to follow the pattern needed any suggestions on the rewrite rules if the first one won't work and how to do the second one?

I've also downloaded the htaccess cheatsheet pdf to learn a little more

Upvotes: 0

Views: 140

Answers (2)

anubhava
anubhava

Reputation: 786291

You need to first have a rule for rewriting images to correct path and then your catch all rule will appear:

DirectoryIndex index.php
Options -MultiViews
RewriteEngine On
RewriteBase /

RewriteRule ^(image)/[^/]+/(.+)$ /$1/$2 [NC,L]

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

Upvotes: 1

arober11
arober11

Reputation: 2019

Something like:

Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?image/user/([a-zA-Z0-9_-]+)/([a-zA-Z0-9.]+)$ /image/picture.php?user=$1&image=$2 [NC,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ profile.php?user=$1 [NC,L]

DirectoryIndex index.php

Upvotes: 0

Related Questions