dweb Posted July 29, 2014 Share Posted July 29, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/290174-rewrite-for-custom-url/ Share on other sites More sharing options...
Stefany93 Posted August 1, 2014 Share Posted August 1, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/290174-rewrite-for-custom-url/#findComment-1486638 Share on other sites More sharing options...
Jacques1 Posted August 2, 2014 Share Posted August 2, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/290174-rewrite-for-custom-url/#findComment-1486649 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.