Jump to content

Rewrite for Custom URL


dweb

Recommended Posts

Hi all

 

I have the following in my .htaccess file

RewriteRule ^([^/]+)/ /$1.php?q=$1 [L,QSA]

 

and this is great because any PHP page I create, such as 'about.php' can be rewritten as '/about/' - so that is all good.

 

the problem is, I also want to let users create their own custom URL's for their profiles in the root of the site, such as '/david-jones/' but because 'david-jones' is not actually a real PHP page (but rather a database entry), I cant use a rewrite rule as far as I can see.

 

Would the best solution be to piggy back the error 404 rewrite rule, so that if an actual PHP page isn't found, it takes them to the 404 error page which actually contains a script to lookup the users details, if the user details are not found, i'd show a "the page cannot be found" message.

 

Or is there a more elegant way to do this?

 

Thanks

Link to comment
Share on other sites

Hmmmm, pretty complex task.

 

I'd say, save the user's selected URL like "www.awesome-website.com/oliver-cromwell in the DB, then write a script to check whether the user had selected a special URL and then set an .htaccess directive to re-write the URL while in reality the page will be like "www.awesome-website.com?user_id=12" 

 

Hope it helps, try that and write here with the errors.

Link to comment
Share on other sites

Letting people create custom directories in the root folder is a rather bad idea, because now you have to worry about name collisions with actual directories. For example, the name “about” is already reserved for your “about” page. And I'm sure there are many others.

 

It's better to create a separate namespace like “yoursite.com/users/john-doe” or “users.yoursite.com/john-doe”. This would also solve your problem immediately, because you could simply map those URLs to a PHP script which displays the corresponding profile.

 

If you absolutely must have your user folders in the root directory on the main domain, this will be more complicated. Either you hard-code all the “real” folders in your .htaccess and maps the rest to a profile script. Or you set up a front controller which processes all requests and decides what to do based on whether or not the URL was registered by a user.

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.