Prettify my URLs with PHP - php

I'm writing a site in PHP that I would like to have structures, nice, and pretty URLs in. I know in one site I had rules setup in the .htaccess, but I have to have some of these URL rules created dynamically, so is there a way to do that in the PHP itself? I feel like Wordpress does this somehow with its pages.
Basically, if I go to this page:
site.com/users/Xan
it will technically be loading:
site.com/users?name=Xan
I can write the patterns (using regex) fairly easily. I just need to know how to make this possible.

Option 1:
To redirect using .htaccess to /users enter this in:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ users?user=$1 [L,QSA]
You can then get your username as $user by simply doing:
$user = $_GET['user'];
Option 2:
If that's not what you're after and you're looking to redirect /users/username to $_GET['page'] in the index.php, stick this into your .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
So let's explode that response in PHP:
$urlarray = explode("/",$_GET['page']);
Now
echo $urlarray[0]; // users
And...
echo $urlarray[1]; // username

Related

Clean url rewriting using .htacces file in apache

I have a website where i do blogging... on my blog.php page i have posts from different categories.
when a user clicks on a link it is being redirect to /blog-details.php page with $_GET['title'] variable.
what I have tried
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
#remove .php from all pages
RewriteRule ^([^\.]+)$ $1.php [NC]
#blog/title-here
RewriteRule ^blog/([A-Za-z0-9-\+]+)/?$ /blog-details.php?title=$1 [NC,L,QSA]
last line gives me something like
xyz.com/blog-details?title=hello-world
what i want to achieve is
xyz.com/blog/hello-world
not like
xyz.com/blog-details?title=hello-world
thank you, looking forward to answers.
An important thing you have to take into consideration when working with .htaccess files, is that you have to check if the path is a directory or file, then proceed to do your action.
I used your code and it worked properly, but simply added the -f and -d checks to make sure that this does not affect other pages.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
#remove .php from all pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC]
#blog/title-here
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/([A-Za-z0-9-\+]+)/?$ /blog-details.php?title=$1 [NC,L,QSA]
To test out your .htaccess file and with some dummy URLs, I'd highly recommend you to check out MadeWithLove
https://htaccess.madewithlove.be/
This will let you have a test input URL and see if you get the proper output URL (the actual URL your server would read).
With that being said, I would recommend you to look into routing for future projects and self-development. A framework I'd recommend checking out is fat-free framework (f3) for PHP. It's very easy to get into, you could learn it in 2 days, and it saves a lot of time in development - by containing routing, ORM, global variables, simplified SQL queries, and plenty of other features that essentially keep you a PHP developer, rather than a framework developer.
use below method:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^blog/([0-9a-zA-Z_=-]+)$ blog-details.php?title=$1 [NC]
</IfModule>
Then you can access the title like $_GET['title'] on the page.

When a user visits www.mysite.com/string can it redirect to my index and use that string?

Currently I have it setup like this so when a user visits www.mysite.com/test.php?u=String the user string will be set as String.
$user = $_GET['u'];
But is it possible to have it if a user searches www.mysite.com/String it would set the user value as the string.
$user
I’m guessing it would require me to edit the .htmlaccess file but I am unsure on how I would do this.
Thankyou in advance
You could make a .htaccess redirect for all requests, and route them to your test.php file, which then assigns the string to your $user variable.
The script would look something like this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /test.php?u=$1 [NC,L,QSA]
I hope you're also considering the security risks of letting a user input any string they want in your script just by visiting mysite.com/String
It's .htaccess not .htmlaccess
If you want to keep the URL as something like:
http://www.example.com/search but, in reality, direct to http://www.example.com/test.php?u=search then you'll probably want a RewriteRule
In this instance, to route everything that's neither a file nor a directory to test.php?u=string
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /test.php?u=$1 [NC,L,QSA]
http://www.example.com/this/is/not/a/directory will be routed to http://www.example.com/test/php?u=this/is/not/a/directory
This needs to be in an .htaccess file at your document root.

add page name in the url using .htaccess

I just started .htaccess so I do not know too much about it, I try to google it but failed so I post my question here, I am sorry if the question is silly for you. My question is.
I use this code in .htaccess page:
RewriteEngine On
RewriteBase /learn/php/htaccess/
RewriteRule ^(.*)/(.*)$ index.php?n=$1&p=$2
when I enter this url:
http://localhost/learn/php/htaccess/page/number
it works fine. but I also want to add index.php (page name) in the url. I tried this:
http://localhost/learn/php/htaccess/index.php/page/number
AND
http://localhost/learn/php/htaccess/index.php?page/number
but failed. How can I do this?
I simply want to do that if I add another page then I am not sure but I have to use this code:
RewriteEngine On
RewriteBase /learn/php/htaccess/
RewriteRule ^(.*)/(.*)$ index.php?n=$1&p=$2
RewriteRule ^(.*)/(.*)$ another.php?x=$1&y=$2
but how can I open the another.php page with this format?
thanks
Try adding a few conditions to your rules:
RewriteEngine On
RewriteBase /learn/php/htaccess/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ index.php?n=$1&p=$2
As for routing to another page, you'll need to distinguish the difference between the two routes. For example, given a url:
http://localhost/learn/php/htaccess/foo/bar
where should it get routed to?
index.php?n=foo&p=bar
or
another.php?x=foo&y=bar
?
htaccess knows nothing about the content, only the URL and a regex pattern to match it, so unless you differentiate between the two, you can't route the same thing to two different scripts.
You can just make it easy like this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?data=$1 [QSA]
in your index.php you put this code
$data = explode("/",$_GET['data']);
$page = $data[0];
$number = $data[1];

Pagination and htaccess

I am looking for some help in my htaccess to better handle my urls when using a pagination function.
Currently my htaccess looks like this:
RewriteEngine On RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?alias=$1 [QSA,NC,L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?alias=$1/$2 [QSA,NC,L]
basically, I have two rules. At first I only had the second one, to direct all requests to my index page, where I then parse out some details of the url in order to show the correct content. I found that it didnt work for urls with only one sub directory, like example.com/bars, but only two directories like, example.com/bars/bar-name, so I added the rule before it.
This has been working great for me. All of my urls look great and the ?alias=bars/bars-name has been completlely hidden and now looks like /bars/bars-name
Recently though I have integrated a pagination php function. This function creates urls that looks like this:
example.com/bars?alias=bars&info=mysql_table_bars&page=2
Basically, 'info' is the table name that the pagination function queries, and 'page' is the current page. It seems 'alias' also started showing in the url once I started using this function.
What I would like is for my pagination to look like these examples:
example.com/bars/page/2
example.com/hotels/page/5
etc...
What do I need to add to my htaccess to handle this situation? Also, is there a way to simplify my existing two rules?
Regarding your original rules. The conditions that you have only get applied to the immediately following RewriteRule, so you need to duplicate them:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?alias=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?alias=$1/$2 [QSA,NC,L]
For the new pagination stuff, firstly, it is really bad to be pulling database table names from the query string, unless they've been validated somehow (which may be what's happening). But even then, it's still an information disclosure problem. So you can use these rules to always remove them:
# This matches the actual request when there's pagination in the query string
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/\?]+)\?alias=([^&\ ]+)&info=.*&page=([0-9]+)
# an optional sanity check, does the request URI match the "alias" param?
RewriteCond %1:%2:%3 ^(.*):\1:(.*)$
# redirect the browser to the URL with the table name removed
RewriteRule ^ /%1/page/%2? [L,R=301]
Then you need rules to internally rewrite them back to the request with the query string:
# rewrite it back to the query string
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/page/([0-9]+) /$1?alias=$1&info=mysql_table_$1&page=$2 [L]

.htaccess issue when creating users profile?

I am using .htaccess code in order to pass pages titles to the url and then retrieve them with php from mysql tables when the page loads.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
I am using this php code to explode the url and get the title:
$passed_url = $_SERVER['REQUEST_URI'];
$passed_url = explode("/", $passed_url);
$passed_url = end($passed_url);
Now I want to create a user profile page like so www.website.com/users/User_name_in_here
how do I check with php if this is a users page or it is just a regular page which can be writer like so: www.webiste.com/Page_title_in_here
is there a better way to do this ?(I am just a starter in php)
One way is to check $passed_url[1]. If it's 'users' then you know you're on the users page.
A better way would be to use index.php as a front controller and pass the request to a controller based on the request uri. there are a number of ways to do this.
Some frameworks map the uri to a class::method. So your url would be changed to /users/view/username and the users::view would be called.
I prefer to write regular expressions and which ever regular expression is matched controls which controller is loaded.
You can actually change your Rewrite Engine .htaccess file this way:
RewriteEngine On
RewriteRule (.*)-(.*)\.ptf$ ./?page=$1&id=$2&%{QUERY_STRING}
RewriteRule ^([^/]*)\.ptf$ ./?page=$1&%{QUERY_STRING} [L]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ ./?page=$1&id=$2&%{QUERY_STRING}
So, this rule, what it exactly does is:
when the URL is www.website.com/Page_title_in_here,
it goes to www.website.com/?page=Page_title_in_here
and when you put this way, www.website.com/users-User_name_in_here,
it redirects to www.website.com/?page=users&id=User_name_in_here
So you can manipulate with the $_GET["page"] and $_GET["page"] variables!
What say? I do this way. Hope this helps you!

Categories