Jump to content

htaccess help - hiding ?username=


slj90

Recommended Posts

There are 2 parts to my question.

 

Firstly, To view a users profile on my site you can type this:

 

www.website.com/username

 

I now want to do the same thing with the gallery page. Like this:

 

www.website.com/gallery/username

 

My question is, do I need to put the gallery.php page in a folder called 'gallery' or do I just need to have the gallery.php page in the htdocs main folder?

 

 

 

My .htaccess currently reads

RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1

What do I need to add to it so that gallery works too?

 

Thanks

Link to comment
Share on other sites

You can put gallery.php anywhere you want. In the main folder makes the most sense.

 

I'm making a couple changes:

RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/?$ profile.php?username=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^gallery/([a-zA-Z0-9_-]+)/?$ gallery.php?username=$1 [L]
1. Note the two (pairs of) RewriteConds: they make sure that you don't accidentally rewrite from an existing file or (more likely) directory.

2. Most of the time you should use the [L] flag so that when one rewriting rule matches it will stop processing the rest of the rules and immediately go with the new URL you told it to use.

 

And a suggestion: decide whether you want to officially have trailing slashes or not - don't do both. You can still redirect from one to the other, of course.

Something like

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [L,R=301]
should be able to handle redirecting away from a trailing slash for pretty much anything you have. Directories still need the trailing slash so those aren't affected.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.