I'll be signing businesses up to advertise on my website, and I want them to have a direct URL for their customers to go to.
Like, instead of www.website.com/page.php?id=324234234,
I want to have www.website.com/businessname
Is there a simple way to do this? I've searched and seen a whole bunch of different things people are trying to do but I haven't seen anything that's the same as what I want to do.
I'm using a VPS, and I want to make sure that I don't open up permissions so that anyone can get in there and mess things up.
Also, these users will not be signing themselves up. I will be doing that.
The simplest way to get my end result is what I'm looking for. Thanks!
Basic URL rewriting could work.
Add to your .htaccess file
RewriteEngine on
RewriteRule ^(.*)$ page.php?businessname=$1 [L]
Then use PHP to rewrite the businessname to the ID of the company / find the data.
Of course .htaccess rewrite rules is a complete science if you need more complex rewriting...
Re-iterating what jtheman said with a little more explanation:
Create a file named .htaccess with the contents:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ page.php?businessname=$1 [L]
You need, of course, the ability to have directory level .htaccess enabled - you're using a VPS so you should be able to do this if it is not already enabled.
So let me explain what each line will do.
RewriteEngine on
Turns on the ability to URL re-write
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Tells Apache not to re-direct files that exist in the directory already
RewriteRule ^(.*)$ page.php?businessname=$1 [L]
This is where the magic happens.
^(.*)$ this part is like a regular expression match. It will tell Apache to collect any URLs that have any characters within them and redirect them to page.php?businessname=(.*)
So, if you post:
www.website.com/stackover
It will really be sending: www.website.com/page.php?businessname=stackover
Then you can just use $_GET[businessname] to dynamically update the page.
Hope this helps!
Related
I have a scenario where I want something similar to Codeigniter.
In Codeigniter my url is like:
http://www.example.com/filename/methodname
Now I want similar thing but using plain core PHP and .htaccess.
How is that possible ?
I want to have a index.php inside my folder and then redirect the http requests accordingly.
Searching the web I found this :
RewriteEngine on
RewriteRule ^([a-z0-9_-]+)\.html$ index.php/page/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|asset|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
But I have little knowledge of .htaccess and don't know how this could help.
So I want an answer with example to understand how this can be achieved using .htaccess.
What would I need to do on my PHP side ?
Routing
Having urls like /filename/methodname is generally called routing. You have half of it done already; what you show in .htaccess is the part that will redirect all traffic towards an index.php file.
# starts rewrite engine
RewriteEngine on
# redirects direct .html page calls to their corresponding pages
RewriteRule ^([a-z0-9_-]+)\.html$ index.php/page/$1 [L]
# for anything that is not a file
RewriteCond %{REQUEST_FILENAME} !-f
# and for anything that is not a directory
RewriteCond %{REQUEST_FILENAME} !-d
# except if it's a robot
RewriteCond $1 !^(index\.php|asset|robots\.txt)
# send all that to index.php
RewriteRule ^(.*)$ index.php/$1 [L]
That index file will then parse the url and call relevant handlers with relevant arguments based on what matched.
How to create one such parser, or router, is beyond the scope of a single answer, but basically depends on the use of $_SERVER['REQUEST_URI'] and an array of urls with their corresponding handlers.
Solution
This is a "solved problem", and while it is interesting to implement such a thing by yourself, I would recommend simply to use a library that does it for you. I personally use Fast-Route, a pretty straightforward library that allows for customization in the way you handle routes, but if you google for "php routers" you will find plenty of them.
Of Filename/Methodname
(opinions follow from here on)
This point should be rethinked. While with psr-4 (and psr-0, and probably psr-whatever) the correspondance between a specific class and its file is that the file is named after the class it contains, I believe it better to not think about this as filename/methodname but rather section/action, or whatever speaks best of what the url actually does.
Moreover, if you start using namespaces (which you should do if your oop code becomes slightly more complicated than a hello world page), you obviously won't pass full namespaces in urls, and they actually are irrelevant to your users.
I'm trying to get right syntax for .htaccess without any result...
I've a URL structured as domain.com/app/public/pageName .
It's working fine but I would "hide" the 'app/public/' part in browsers, basically doing something like:
[real URL] domain.com/app/public/pageName -> domain.com/pageName [what users type and see in browsers]
I think in that way it should be more readable and seo-friendly.
As I understood from docs (and maybe it's wrong because it's not working...) I should tell to Apache to map/redirect all URL like domain.com/pageName to domain.com/app/public/pageName , but only internally, in order to show the minimal URL in users' browsers.
Right now I have something like:
RewriteEngine on
#RewriteBase /app/public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ https://localhost/app/public/index.php?url=$1 [QSA,L]
(I'm using full URL with https://... in order to get something that will be quick and easy to adapt when I upload all to my hosting, is it right?).
Problem is that RewriteRule actually change the URL, because it perform a redirect and URL rewrite it's not handle internally.
So, first of all: is it possible what I'm trying to do? If so, how can I handle the URL rewrite only internally?
Everything should be uploaded to a shared hosting, so I don't have other than .htaccess.
Anyway, I can consider to upgrade to a vps if there are not other possibilities...
Thanks!
==============
EDIT (should be more clear now)
tl;dr version:
I'm looking for a method that let users to type domain.com/pageName (and they will see that address in their browsers) and rewrite internally that URL in order to point to domain.com/app/public/pageName.
==============
More: after /app/public/ there can be an arbitrary number of elements, separated by / . All of these elements are appended at the end of the URL after index.php. At the end URL looks like:
domain/app/public/index.php?url=lot/of/elements/here
This is already working with the RewriteRule posted above, I would keep that too.
Thanks!
This is working fine for me, Hope it will work for you as well.
Check .htaccess here
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/app/public
RewriteRule ^app/public/(.*)$ /$1 [L,QSA]
Just for reference, I found a solution, maybe will be usefull for someone.
Basically I moved .htaccess to the root server (instead of /app/public directory) and changed the RewriteRule as follow:
RewriteEngine on
RewriteBase /app/public/
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [PT]
Now it's working (at least on localhost).
What do you think? Are there any side effects with this config?
I am deciding to create separate profile links for each user who registers on the website. I am using the .htaccess file RewriteRule to achieve this. The url I want to achieve should look something like www.domain.com/username. (I don't want www.domain.com/users/username)
Now the problem is, if I add a rule like RewriteRule ^(.*)$ /users.php?username=$1
it will matchup all URL addresses for www.domain.com, which may direct to any path. Even if I would like to visit www.domain.com/about page, it will be redirected to
www.domain.com/users.php?username=about, which I don't want. (Even if the requests was www.domain.com/products/abc)
I could apply filters in my users.php to filter such usernames, or if a username is not found in database, redirect to the page, but this means I have to change filters every time I decide to add a new page in my directory (in this case, any). Also, all traffic will be directed through 1 page, which may cause a performance problem.
Please suggest a way to achieve this, as There are websites that work like this. A possible RewriteRule or PHP code to achieve this.
You can use this rule in your root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)/?$ /users.php?username=$1 [L,QSA]
I always use just simple rewrite as below:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)(.*)/?$ index.php
All traffic is redirected to index.php and using php I can run specific controllers depending on url. You should also think about. Almost all frameworks use such rule.
Then in your PHP file you should examine
$_SERVER['REQUEST_URI']
variable to route request to specific controllers
I'm trying to do a query on some information if someone types, for example:
http://domain.com/abracadabra
If someone enters in this address, I want it to go to:
search.php?query=abracadabra
I've done rewrites with queries before, but not from the root.
Any help would be great!
Would it be possible to check if the string being searched is numerical and send it to a different PHP page? Maybe link to a directory with a new .htaccess?
Doing it from the root is no different than doing it anywhere else. The below will turn every request for a resource (that doesn't exist in the file system) to search.php.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ search.php?query=$1 [L,QSA]
I am trying to change my url type with .htaccess but I have a few problems. I tried a few online tools but they even do not work for me. So here what I am trying to do;
I have pages like http://mydomain.com/profile.php?u=newuser and I want to make as this: http://mydomain.com/newuser but so far I could not achieve it.
Here also what I have tried;
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) profile.php?u=$1
After making changes in .htaccess, do I also have to make any changes in my php files? Also, when try to open http://mydomain.com/newuser I noticed that some of my images on the page are disappearing, what would be the reason for that? Thank you so much guys!
You've gotten quite far, but now you're rewriting every possible url in your domain to profile.php, including stuff like /images/logo.jpg for example.
The question is, what do you want to do with this? An easy way to go about is changing it to this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) profile.php?u=$1
The added 'RewriteCond' causes the rewriteengine to only rewrite urls that don't exist on the server, so your images will show up fine.
Personally, I think it might be better to add a /profile/ prefix to all your profile urls:
Options +FollowSymLinks
RewriteEngine on
RewriteRule profile/(.*) profile.php?u=$1
This will allow you to add new rewrite rules in the future, if you need them; it will also not give you issues if one of your users decides to go for a username called 'profile.php' or anything else that clashes with the existing urls on the server.
This is because you're sending every request to profile.php (.*). This is going to affect all the requests for images, resources, etc.
Add this line above your rule to exclude "real" resources:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d