What is the best way to create user vanity URLs under a LAMP configuration?
For example, a user profile page could be accessed as follows:
http://www.website.com/profile.php?id=1
Now, if a user enters a "vanity URL" for their profile I would want the the vanity URL to load the page above.
For example, if a user selects "i.am.a.user" as their vanity URL and their user id in the database is 1 then http://www.website.com/profile.php?id=1 would be accessible by that URL and http://www.website.com/i.am.a.user .
I'm aware of mod rewrites in .htaccess but not sure how that would work here.
As I mentioned my site is in PHP, MySQL, Linux and Apache.
Thanks.
Say your other pages had specific URLs that you could check against, the following should help.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_]*)$ /profile.php?user=$1 [L]
This helps to maintain current URLs, while allowing for the user shortcut URLs. Also, the RewriteRule will only match URLs that don't contain a /, which will help protect against non-intended redirects. So,
/i-am-a-user -> MATCHES
/i_am_a_user -> MATCHES
/i-!am-a-user -> NOT MATCHED
/i.am.a.user -> NOT MATCHED
/i.am.a.user/ -> NOT MATCHED
/some/page/ -> NOT MATCHED
/doesnotexist.php -> NOT MATCHED
/doesnotexist.html -> NOT MATCHED
Hope that helps.
EDIT
I've updated the rules above so that actual files/directories aren't redirected as well as making sure that any .php or .html file is not sent to profile.php either.
Rewrite for site.com/user/USERNAME:
In your root web directory, place a .htaccess file:
RewriteEngine on
RewriteRule ^user/(.+)$ profile.php?name=$1 [L]
This routes all requests that starts with "user" to profile.php and pass the URI to $_GET['name']. This method is preferred if you have a lot of files / directories / other rewrites.
Rewrite for site.com/USERNAME:
RewriteEngine on
# if directory or file exists, ignore
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ profile.php?name=$1 [L]
This routes to profile.php ONLY if the requesting file or directory does not exists, AND when the request URI is not empty (ie, www.site.com)
PHP backend
Now in profile.php, you can have something like this:
if (!empty($_GET['name'])
$user = ... // get user by username
else
$user = ... // get user by id
First setup your .htaccess file to send all requests for files and directories that don't exist to a single php file:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) router.php [NC,L]
Then inside your router.php, look at $_SERVER['REQUEST_URI'] to get the username that you can then use in your query to get the data about the user.
This assumes that all URLs that are not user profile pages exist as physical files on your server.
If that's not the case, you can do some logic in router.php to decide what to do on each request. Do a google search for url routing in php and you'll get plenty of examples.
Well, you could solve this using an apache RewriteMap as well of course. The RewriteMap can be a plain text file (that you update regularly based on what your users enter), or alternatively you could point it to a script (Perl, PHP, whatever suits you) to do the rewriting for you.
For a quick summary on how to set this up using PHP refer to Using a MySQL database to control mod_rewrite via PHP.
Related
I have set up my Virtual Host in a way that it points all subdomains to their respective directories. For example, ok.domain.com points to domain.com/ok.
ok.domain.com/page points to domain.com/ok/page.
However, I want users to be able to have their own custom domains point to their directories.
I have a PHP file containing keys that point a user's custom domain to whichever directory belongs to them.
"someusersite.com"=>"bob",
"anotheruserdomain.co"=>"test"
Is there a rewrite rule I could use that sends the URL the user is on to this PHP file and then points to whatever it returns.
So if the user is on someusersite.com, this domain should be sent to the PHP file which in turn will echo out the value bob. Now the user should be pointed to bob.
Each of these custom domains will have A Records put in place.
I have tried letting users point their domain to their subdomain via a CNAME record but that doesn't work - it just points to the root site.
Thank you!
Redirect all to index.php htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]
Redirect all access from a folder to a PHP file
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^doc\/(.*)\.html$ download.php?r=$1 [L,R=301]
Hey i was wondering how people made websites that are dynamic in the sense that they can use URL's like /user/kappa in their website without having the actual folder in the files.
I am not quite sure how they do this but i have been looking around for a while and couldn't find an example so i thought i may ask here!
https://domain.tld/user/kappa
like how it says /user/kappa but doesn't make the folders physically
is there a way i can do this using PHP?
Any help would be appreciated.
To achieve this you can use mod_rewrite.
You can add a few lines to your .htaccess file to set it up. What it is doing is taking a PHP file with a set of variables and re-writing the URI based on rules you set out. For example, you may have one index.php file that handles all your content with different variables. ie:
example.com/index.php?page=news&id=34
You could use mod rewrite to change this to
example.com/news/title.html
As an example this is some of the code the CMS Joomla uses for its URLs. I also found this page which goes over some basic examples. http://www.the-art-of-web.com/system/rewrite/1/
RewriteEngine On
RewriteRule .* index.php [F]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index\.php
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule .* index.php [L]
I was trying to prettify URLs for dynamically generated pages on my website, so that when a user visited the virtual URL topicview/interesting-user-friendly-text he would really be seeing, under the hood, topicview.php?topicid=123
I added the necessary code to my .htaccess file to replace the topicview/interesting-text part of the URL with topicview.php?topicname=interesting-test, but the Regex kept misfiring.
So, I changed the Regex to return the entire URL so I could see why it wasn't working with this code:
#Allow for topicview/topic-name URLs
RewriteRule (.+)$ topicview.php?topicname=$1 [L]
I then visited topicview/user-friendly-text. I'm not sure if this is unique to Network Solutions hosting, however, when I examined the topicname GET paramter, I got this string in return:
data/1/2/323/232/823238/user/999999/htdocs/topicview.php
This URL was not displayed in the topicname GET parameter, just a regular file, like index.php or topicview.php, if I just visit the URL index.php or topicview.php
Why is the URL internally represented like this to the Apache server, and how can I rewrite my mod_rewrite code to get a more user friendly virtual URL for the topicview.php?topicid=1 pages?
thanks
For the friendly URL try this in your .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?topicview/([^/.*]+)/?$ topicview.php?topicname=$1 [L,QSA]
The pattern matched in a RewriteRule is normally the similar to the REQUEST_URI but the behaviour you describe suggests it is matching against the REQUEST_FILENAME or something similar which is the file path including the full document root.
This would suggest your RewriteRule is not in your .htaccess file but instead in your or rules in the Apache config files, correct?
Instead you should try getting the value you want using a RewriteCond so you can guarantee you are matching against the REQUEST_URI, for example:
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule . topicview.php?topicname=%1 [L]
Note the %1 rather than $1 which allows you to use values captured in the RewriteCond.
I'm looking to handle the URL's except homepage with a common PHP file. This is just like a PHP $_GET request except the difference that there would be no parameter. It'll be just like a file.
Ex- http://localhost/ - This should be managed by index.php file as usual.
http://localhost/ANYTHINGHERE - This should be thrown to a custom PHP file which would then decide what to do.
Actually, I'm working on a project where I need to hide the URL information from the users. So, the file that would manage the ANYTHINGHERE URL would actually access a directory localhost/i/.
Thanks and waiting for best response!
To achieve this you need two parts:
First: .htaccess which redirects all accesses to your domain passed to a php script (index.php here):
RewriteEngine On
RewriteRule (.*) index.php/$1
Second: In index.php you get the user-entered URI as $_SERVER['PATH_INFO'] (starting with /)
This, however, makes all requests to go through the index.php script (depending on the location of index.php you could also get an endless recursion, so read on ;) ). Normally one doesn't want that (e.g., images should be served directly by the web server). Thus, one normally uses (i.e., existing directories, files and links are served by the web server directly):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^(.*)$ - [NC,L]
RewriteRule (.*) index.php/$1
If this should take place in a subdirectory you need to add RewriteBase /subdirectory directly after RewriteEngine On.
If you don't want to use $_SERVER['PATH_INFO']you can also use RewriteRule (.*) index.php?url=$1 [QSA], then you get the user entered URI as $_GET['url'].
This requires mod_rewrite to be loaded on the server.
See http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html.
This is what I would like to do with htaccess (or whatever you suggest):
I want to allow users to publish pages,photos,videos,... and give them each a subdomain of their choice (ex: john.mysite.com).
Here's how a URL can be -really Tumblr like (it would be great if it'd work like Tumblr :)-
john.mysite.com/photos
john.mysite.com/photos/album/123456789
john.mysite.com/news/123456789
john.mysite.com/news/category/123456789
Using htaccess I can make whatevertheusernameis.mysite.com be served by mysite.com/user.php
Then in user.php I can see who the user is analyzing HTTP_HOST (getting 'whatevertheusernameis' and checking my DB) and I can know what to show (photos,videos,news,pages,...) reading REQUEST_URI.
I would like certain URL to work as follow:
john.mysite.com/ajax/ajax.php -> mysite.com/ajax/ajax.php
john.mysite.com/images/img.png -> mysite.com/images/img.png
but if, for example, I'm in john.mysite.com/photos/album/123456789/ this will be served as john.mysite.com/photos/album/123456789/ajax/ajax.php that, of course, won't work...
Can anyone please help me? Thanks.
What you have to do is add a condition before all your other rewrites:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ...
Those directives will tell Apache to always serve actual files (f), directories (d) and symbolic links (l) and use the sub-domain to identify the location of the user data (photos, etc).