This question already has answers here:
How to Implement URL Routing in PHP
(2 answers)
Closed 9 years ago.
Is there a way to make this URL:
http://readme.com/read?blog=10092
look like this:
http://readme.com/read/blog/10092
Using PHP?
It is not possible via PHP itself. Even the above example (read?blog) is not a php-only solution.
PHP is a parsed file. The webserver parses a .php script and displays it to the viewer. So you have to configure your server (Apach for example) to use the correct php file for the request. The most common solution is mod_rewrite (http://httpd.apache.org/docs/current/mod/mod_rewrite.html an lots of tutorials). You will have to edit yout .httaccess file and create configuration options.
One technique is by rewriting the url using .htaccess. Create a .htaccess (There's a dot in front of the file name!) file inside of your root folder and place the following code inside:
Options +FollowSymLinks
RewriteEngine on
RewriteRule /blog/(.*)/ read?blog=$1
Now you can go to the same page using the url :
http://readme.com/read/blog/10092
It looks like you need a PHP framework, like CakePHP or CodeIgniter. One of their main feature is the routine mechanism. However, you must first understand some Object-Oriented Programming in PHP.
Its more complicated with plain php, you have to use htaccess too. See this example:
RewriteEngine on
RewriteRule ^/read/blog/([0-9]+)$ /read?blog=$1
Url rewriting is powered by .htaccess and inside it, regular expressions. If you want to know more about regular expressions, see wikipedia: https://en.wikipedia.org/wiki/Regular_expression
Related
This question already has answers here:
How do I add PHP code/file to HTML(.html) files?
(12 answers)
Closed 8 years ago.
I am creating a website for my organization that also has some web applications that run server side via CGI using Python. I am wanting a little bit more of a intuitive front end framework. I am also more familiar with Scripting languages then web languages. I have chosen Bootstrap as my frame work for it's user base and bound of documentation.
Now I am understanding the website authentication in regards to PHP and mySQL. But I have a question that I have not been able to find the answer to.
How do I set up website authentication via PHP and mySQL but not have every page end in .php?
For example is it possible to somehow insert this CheckLogin code at the beginning of but inside the html code of a webpage that is .html instead of .php:
<?PHP
require_once("./include/membersite_config.php");
if(!$fgmembersite->CheckLogin())
{
$fgmembersite->RedirectToURL("login.php");
exit;
}
?>
The CheckLogin code:
function CheckLogin()
{
session_start();
$sessionvar = $this->GetLoginSessionVar();
if(empty($_SESSION[$sessionvar]))
{
return false;
}
return true;
I am referencing this tutorial:http://www.html-form-guide.com/php-form/php-login-form.html
Update: I am using Apache.
I am looking for some guidance besides just the name of the technology, something with a example or tutorial as again I am very new to webside programming and I would benefit most from some real world type of examples.
EDIT: I am also looking for alternatives that would be best suited for CGI type of websites. Anything in the realm of Apache and website authentication based around integration with CGI.
This question was marked as duplicate but I disagree as it has more caveats do to the CGI nature instead of how to simply insert PHP into HTML. The most common solution found when researching "Web site Authentication" is PHP. Hence why asking how to make a CGI website turn by turning PHP pages into HTML pages for the sake of simplicity of using CGI. If there are easier ways to do this by all means please share.
There's a few alternatives. One is to have Apache parse .html files as .php files by adding this to your .htaccess:
AddType application/x-httpd-php .html
Then you could simply add your script to the top of your .html files and it would work. However a cleaner solution would be to use mod_rewrite. For example, you could route www.example.com/page to www.example.com/page.php with
# .htaccess
# Enable Rewriting
RewriteEngine on
# Rewrite URLs
RewriteRule ^/(.+)/?$ $1.php
Or you could have one file as the router:
# .htaccess
# Enable Rewriting
RewriteEngine on
# Rewrite URLs
RewriteRule ^/(.+)/?$ index.php?page=$1
This version would pass whatever comes after www.example.com/ as a $_GET['page'] variable to your index.php file.
Note: I didn't test these, but they should work.
Here's a pretty nice tutorial for some more info: http://code.tutsplus.com/tutorials/an-in-depth-guide-to-mod_rewrite-for-apache--net-6708
as Halcyon wrote, it is not possible to combine your Authentification with .php and .html, because in .html you can't extract the session vars.
But as he wrote, you can use mod_rewrite to display the page not as ".php" but as ".html".
Regards
Mike
This question is related to PHP
How do I make a request to a directory on my server (that doesn't exist) become treated as a variable.. For example:
domain.com/username will really be a request to domain.com/profile.php?user=username
Is this even possible? Or how does YouTube/Twitter/Facebook do it?
Jeff, this is called "friendly URL" and is done with url rewrite. I recommend reading this documentation: http://httpd.apache.org/docs/current/mod/mod_rewrite.html.
If you are not familiar with regular expressions you shoud read http://www.php.net/manual/en/reference.pcre.pattern.syntax.php
If you using apache as web server create a .htaccess file on your directory with following content to achieve this
RewriteEngine On
RewriteRule ^[a-zA-Z0-9_.-]+$ profile.php?user=$1 [L]
Your looking for mod rewrite. Heres a short tutorial
Most PHP frameworks have libraries that make this much easier, your best bet is probably to use one of them.
How do you configure Apache and/or PHP to be able to access PHP scripts without the .php extension? I have seen PHP scripts executed without the .php extension. I don't mean executing 'script' as a PHP file, I mean executing 'domain.com/script' as a PHP file where 'script.php' exists as a file, but you are able to access it without using the extension. Does anybody know how to configure this?
I AM USING A CPANEL HOSTING!
WHERE TO WRITE THE mod_rewrite? I HAVE A .htaccess file with code # Do not remove this line or mod_rewrite rules and search engine friendly URLs will stop working RewriteBase /
Several basic ways:
Use MultiViews. Automatically converts /foo => /foo.php (among other things)
Use mod_rewrite to remove PHP extensions
Use mod_rewrite to direct all traffic to a single dispatcher script, which inspects the URL and performs the proper action by including files / calling class methods, etc.
Generally that's done in Apache via mod_rewrite.
Here's a guide: http://wettone.com/code/clean-urls
Such a rewriting doesn't make too much sense.
If you want (SEO-firiendly|human-readable) URLs, you have to use complete set of rewrite rules, not just removing extension.
Otherwise there would be no point in such a configuration change
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Rewrite all queries to not need the .php extension using a mod_rewrite RewriteRule
I am learning how to create a website, and would like some help.
I have this path in the wamp folder: C:\wamp\www\Personal_site\Root_fold\index.php (the main file starts here).
I want to have cleanurls for this file: C:\wamp\www\Personal_site\Root_fold\Tutorials\C_sharp\C_loginapp.php, and I want it to show up as C:\wamp\www\Personal_site\Root_fold\Tutorials\C_sharp\C_loginapp - the same file, but without the .php on the end.
I also want this to happen to every other file that I have in my website - even the index.php - without repeating the rewritecond for each and every file. How do I do that?
By the way, I am running this locally on my computer - no hosting service or anything like that - and also I am creating the .htaccess file by opening it in notepad and saving the file with the name ".htacess", which means it forces the file to save with the .htacess extension.
Clean URLs go beyond just hiding the .php extension. It also needs to incorporate query parameters, so that instead of /article?title=foobar it looks like /article/foobar.
This problem was solved over and over and over again. Please don't participate in the PHP community's pervasive Not invented here syndrome and use one of the existing solutions available. It will make you a better programmer in general, because today's programming is about artfully combining existing components with your unique domain logic instead of writing everything from scratch.
UPDATE
Since you are just starting, it might be hard for you to start using a full-blown framework like Symfony2 — but I suggest to use it when you'll be more comfortable with PHP.
For now, the Silex micro-framework might be a good start for you. It's very easy to start with. As a simple example, here is your index.php file:
require_once __DIR__.'/silex.phar';
$app = new Silex\Application();
$app->get('/hello/{name}', function($name) use($app) {
return 'Hello '.$app->escape($name);
});
$app->run();
And this is your .htaccess:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
This is a complete app written in Silex. When you go to <yourhost>/hello/JackyBoi it will respond with Hello JackyBoi.
You can do more advanced stuff with Silex — details are in the docs. When you outgrow it, you can naturally graduate to its bigger brother Symfony2.
Add this to your .htacceess:
Options +MultiViews
Remove all the rewriterules. Then you can use URLs (not filenames!!) without the .php extension.
I have a website that passes some GET variables to different pages in PHP. My issue is that now I have a url with variables i.e. index.php?category=categoryname and that's not very memorable.
Is there any way I can change the URL to something like /categoryname instead without duplicating the page and storing in folders? But also allow users to type in /categoryname and be redirected to the correct page?
.htaccess Apache mod_rewrite, almost every professional dynamic website uses this method (like stackoverflow).
The method is fully explained in this article far better then I could ever explain it in this answer box.
You should look into writing some apache Mod_Rewrite rules in a .htaccess file.
The solution is discussed here:
this is done by the rewrite module of apache and this handles regular
expressions. You have to put a rule
like this in your .htaccess file on
the root of your website:
RewriteRule ^cat/([0-9]+)$
/index.php?category=$1
^ means the start of the url after
www.example.com/ $ means the end of
the page.
www.example.com/cat/123
will be converted by the server to:
www.example.com/index.php?category=123
In PHP you use the normal $_GET['id']
variable. The rewrite module must be
enabled by apache... This is mostly
used to make the url format
independent of the serverside
scripting language so the .php in the
url is not logical. Thats why i
changed it to product/ . The .htaccess
starts with
RewriteEngine On Options
+FollowSymLinks RewriteBase / Here all the rewrite rules.. ...