Create a page without placing .php at the end? - php

I was looking for ways to mimic something I've seen, however I'm really not even sure where to start or how to search for it.
Lets say my page was:
foo.com/ and my index page could take an argument of: index.php?id=5
What I'm wanting to do is create the following:
foo.com/5/ rather than placing index.php?id=5 just use the webstring to pass in the parameters, to hide not only the fact its a PHP page, but to clean up the url a bit more.
Is this possible?
Cheers

You'll want to look into URL rewriting. With the commonly used Apache webserver, this is accomplished with mod_rewrite.

or /?5/123/
and in php parse the query string if rewrite is not available

Something like this should suit:
RewriteRule ^pages/([A-Za-z_-]*)(/?)$ /index.php?page=$1
Broken down, we're looking for a URL that starts with pages, has any combination of letters, underscores and hyphens, and an optional trailing forward slash, and passing that to /index.php to handle.

Yes Mod_rewrite is best option, you can create .htaccess file. if you do not want the write a custom function which will handle the your url.

Related

htaccess RewriteRule url frendly searches

I have a .htaccess file with this in it:
RewriteRule ^search/([a-zA-Z]+)$ index.php?page=search&search=$1
So basically it sends URLs like this:
url.net/search/this
To this:
url.net/?page=search&search=this
But when I send it a URL like:
url.net/search/this+search
I get an error returned as it doesn't know how to deal with +search bit.
Is there a way I can get it to include the + between words when the user clicks search?
I want it so that if the user types i+want+this+or+that or this+is+what+i+want+to+find, so no mater how long it is, it knows how to handle the parse to $_GET['search'] parameter.
You should be able to just include it in the regex...just remember to escape it,
RewriteRule ^search/([a-zA-Z\+]+)$ index.php?page=search&search=$1
Try this regex for the rewire rule:
RewriteRule ^search/([a-zA-Z].+)$ index.php?page=search&search=$1
Note the . before the + sign. Works as a regex here on this live PHP regex site. Yes, I know this is an Apache rewrite rule & PHP has no role at this stage, but basic regex logic should remain the same.

PHP/Apache - Building a different URL, can it be done?

I'm looking into how to build a different URL, and I was wondering if it is possible to replace the query-string/question mark symbol PHP uses in URLs which is the '?' into something else? For example an exclamation mark '!'
www.example.com!foo=1
Is this possible?
Sure, but you'll need rewrite rules to tell the server how to serve them. And PHP won't parse them automatically for you, so $_GET/$_POST/$_REQUEST variables won't get set and you'll have to parse them yourself from $_SERVER['REQUEST_URI'].
You don't have to use mod_rewrite for this, because you can still interpret the url with $_SERVER["PATH_INFO"] or $_SERVER["REQUEST_URI"] depending on your configuration. Just split it at the ! with explode and parse it with parse_str. It is your decision if this makes sense.
You should be able to replace it by htaccess too but I am unable to test it because I have no apache running atm. Must be something like this.
RewriteEngine on
RewriteBase /
RewriteRule ^!(.*)$ http://domain.com/?$1 [R,NC]
If this works, remove the "R," from the rule.

Rewrite URL in PHP

I would like to rewrite the following URL
www.mysite.com/mypage.php?userid=ca49b6ff-9e90-446e-8a92-38804f3405e7&roleid=037a0e55-d10e-4302-951e-a7864f5e563e
to
www.mysite.com/mypage/userid/ca49b6ff-9e90-446e-8a92-38804f3405e7/roleid/037a0e55-d10e-4302-951e-a7864f5e563e
The problem here is that the php file can be anything. Do i have to specify rules for each page on the .htaccess file?
how can i do this using the rewrite engine in php?
To get the rewrite rule to work, you have to add this to your apache configs (in the virtualhost block):
RewriteEngine On
RewriteRule ^([^/]*)/userid/([^/]*)/roleid/(.*)$ /$1.php?userid=$2&roleid=$3 [L,NS]
RewriteRule basically accepts two arguments. The first one is a regex describing what it should match. Here it is looking for the user requesting a url like /<mypage>/<pid>/roleid/<rid>. The second argument is where it should actually go on your server to do the request (in this case, it is your php file that is doing the request). It refers back to the groups in the regex using $1, $2, and $3.
RewriteEngine on
RewriteBase /
RewriteRule ^mypage\/userid\/(([a-z0-9]).+)\/roleid\/(([a-z0-9]).+)$ www.mysite.com/mypage.php?userid=$1&roleid=$2
No you don't need a separate rule for every php file, you can make the filename variable in your regex something like this:
RewriteRule ^(a-z0-9)/userid/([a-z0-9].+)/roleid/([a-z0-9].+)$ $1.php?userid=$2&roleid=$3
If you want to rewrite the latter URL that is entered in the browser TO the first format, you would want to use a .htaccess file.
However, if you want to produce the pretty URLs in PHP (e.g. for use in link tags), then you have two options.
First, you could simply build the URL directly (instead of converting) which in my opinion is preferred.
Second, you could rewrite the first (ugly) URL to the pretty latter URL. You would then need to use preg_replace() in PHP. See http://php.net/manual/en/function.preg-replace.php for more info. Basically, you would want to use something like
$rewrittenurl = preg_replace("#mysite\.com\/mypage.php?userid=(([a-z0-9\-]).+)\&roleid=(([a-z0-9\-]).+)$", "mysite.com/userid/$1/roleid/$2", $firsturl);
Good luck!

htaccess regular expression with a "/"

I'm having a brain cramp. I'm using htaccess to rewrite a page and sometimes the variable that gets passed through will have a / (forward slash) in the variable. Sometimes there will be a slash and sometimes there won't but it is super important that all of this is treated as one variable. I'd really rather not reprogram all my pages with a str_replace() to switch a - for a / and then make a call to a database. For example:
http://www.example.com/accounting/finance.htm
Accounting/Finance is one variable that I need.....it is not in an accounting directory and then there's a page called finance.htm in accounting. So far I've got something like
RewriteRule ^([A-Za-z]+.*[A-Za-z]*)\.htm$ mypage.php?page=$1 [L,NC]
But it doesn't like it.
Can someone help me out?
Thanks in advance.
REPLY TO COMMENTS/ANSWERS
The specific rule that I'm looking for is something like this.....
[start of string]...1 or more letters...[possibility of a / followed by 1 or more letters].htm[end of string]
The two answers given below aren't working...I'm pretty sure it keeps treating it as a directory and not an actual "filename". As soon as I remove the forward slash the page works just fine...
If i get you right, you just need this one:
([A-Za-z/]*)\.htm
it should work with every combination of / or not-/
e.g.
accounting/finance.htm
test.htm
A slash is just another character. Apart from that, your regexp looks unnecessarily complex. For instance, .*[A-Za-z]* is not different from .* and also [A-Za-z] can be shortened to [a-z] if you use the [NC] flag.
Your precise rules are not entirely clear, but you probably want something on this line:
RewriteRule ^([a-z/]+)\.htm mypage.php?page=$1

PHP - How to add a pages title to the URL? And how to create a clean url using PHP

I was wondering how can I create clean urls using PHP. Do I do this all in PHP or do I need to use mod_rewrite in some way? Can someone explain this to me in laymans terms?
Here is my current url a element link and how it looks in the browser
http://www.example.com/members/1/posts/page.php?aid=123
But I want it to read the pages title.
http://www.example.com/members/1/posts/title-of-current-page/
First you need to generate "title-of-current-page" from PHP, using this function eg:
function google($string){
$string = strtolower($string);
$string = preg_replace('/[^a-zA-Z0-9]/i','-',$string);
$string = preg_replace("/(-){2,}/",'$1',$string);
return $string;
}
Second thing, you need to make a rewrite, but you should keep aid in form of "/123-title-of-current-page"
Rewrite would go something like this (I am ignoring your entire URL)
RewriteRule ^([0-9]+)-(.*?)$ page.php?aid=$1 [L,QSA]
You can do this using mod_rewrite:
You'll need to edit a file called .htaccess at the top level of your web folder. This is where you can specify certain settings to control the way Apache accesses items in this folder and below.
First things first. Let's turn on mod_rewrite: RewriteEngine On
RewriteRule ^([a-z]+)/([a-z\-]+)$ /$1/$2.php [L]
The rule matches any URL which is formed of lower case letters, followed by a /, then more lower case letters and/or hyphens, and appends .php to the end. It keeps track of anything wrapped in brackets () and refers to them later as $1 and $2, i.e. the first and second match. So if someone visits these URLs:
http://example.com/weblog/archive
it will be converted to following:
http://example.com/weblog/archive.php
You will find more details on :
http://wettone.com/code/clean-urls
You have to use a rewrite to direct all requests to an existing php file, otherwise you get all 404 not found errors because you are trying to get a page that simply is not there.
Unless you rewrite your 404 page to handle all requests and you definitely donĀ“t want to go there...

Categories