Can I make a dynamic .htaccess file? - php

Can I make my .htaccess be generated with php?
I would like to use php to dynamicly create my htaccess file from information from the database.
It would save me the trouble of making a new .htaccess file whenever I change a bit of my code.

You can read and write files with the file system functions, for example:
$data = <<<EOF
RewriteEngine on
RewriteRule …
EOF;
file_put_contents('.htaccess', $data);
But it would be more flexible if you use one static rule that redirect the requests to your PHP file that then does the rest.

Can you? Sure. You can create files in the filesystem with PHP so long as you have permissions.
Should you? Well, if you are only using PHP in "command-line" mode, I suppose it's as good as another scripting language. If you are doing so from a website, I would say this is a bad idea for security reasons. Another technology might be more appropriate (if you originally had XML, then XSLT would come to mind, but you're using mysql, so you'll need some other kind of script).

You definitely can generate your .htaccess with PHP if you had a backend script to do it - say in your admin area. But it seems risky and potentially unnecessary? if something goes wrong, your site will be down until you fix it - that potentially means the page you're using to generate it. Why not just create an interface that generates the new rules for you from a database, then you can have a quick look over and copy them in?
What exactly is it that you're trying to update?

Related

how to do a URL pathing(?) & .php vs .html

".com/watch?v=M0IDGz0QKDw" I'm a beginner and I wanna know how to do this. Can someone point me out onto where I can learn this url pathing?
And also if .php is way better than .html because php is connected to the database then what is this ".com/messages" weren't it supposed to be ".com/messages.php"
note that messages gets all messages to the database.
First -> ".com/watch?v=M0IDGz0QKDw"
it is that there is v means some variable with unique number.
And watch means in MVC architecture we have route.config file there we will configure the watch.php file.So the routing URL is like that.
Second : Yes always better to use in MVC architecture .
So try to learn MVC architecture by some frameworks like laravel, Codeigniter etc.it may help you out.
".com/watch?v=M0IDGz0QKDw"
watch is actually watch.php or watch.html, file extension is hidden from URL display.
This setting is depends on framework or .htaccess setting.
You can refer to https://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/
v=M0IDGz0QKDw
v is variable name, while M0IDGz0QKDw is value.
In this case, watch page is using $_GET method to collect data from URL.
You can learn that from https://www.w3schools.com/php/php_forms.asp
HTML and PHP are different language and serve different purpose.
In short, HTML handle display, while PHP handle business flow and logic.
I would suggest you learn how to manage data with PHP first, then only proceed to configure .htaccess for hiding file extension. It will be ease for you to do troubleshoot during coding.

A Dynamic website using PHP

I am a beginner PHP programmer. I searched google for a "Dynamic PHP website tutorials". I found some stuff. They use $_GET variable to make the website dynamic, so the URL's appear like this:
example.com/?page=home
example.com/?page=about
example.com/?page=Downloads
and so on...
But most of the dynamic websites that I found on the internet has links like this:
example.com
example.com/about
example.com/download and so on....
So how do they do so ?? Have they got folders for all the catogories ?? And Also some websites have article URLs (eg : example.com/articles/posts/2010/article1.php). It would be a reall mess if they've got folders for all items. If not then How ?? Can someone give an example please ?
If you're using apache then read: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
If you're using IIS then read: http://www.iis.net/downloads/microsoft/url-rewrite
In order to use the $_GET variable, it must be in the query string (or being routed through some other means that isn't 'default').
For example, the URLs you're using would become.
example.com/?page=home
example.com/?page=about
example.com/?page=Downloads
Additionally, you can rewrite URLs using the .htaccess file (http://httpd.apache.org/docs/2.0/misc/rewriteguide.html)
You are interested in page routing.
htaccess and MVC routing may start you down the correct path :)
To echo everyone else, it's called a url rewite.
For example, the url
http://example.com/index.php?ext=blog&cat=news&date=12122012
can be rewritten as
http://example.com/blog/news/12-12-2012
This isn't automatic, it requires defining the patterns used for understanding the new URL in a file called .htaccess which usually resides in the servers root directory. Note that the preceding '.' in the filename makes it a hidden file.
When I was first getting used to PHP i found the site http://phpbuilder.com a great help. They have a lot of articles, and a forum that is fairly nice to noobies. http://devshed.com is a good site too, and has a large amount of information on subjects outside of PHP.
You can achieve that affect with folders, but most use rewrites (Apache). It's a bit too broad of a subject to go in to here, but if you just search for rewrite tutorials you'll find some pretty quickly.
The $_GET is only to get variables from the URL. While this can be used to make sites dynamic, this is a technique which is usually frowned upon.
With rewrites, you basically have a URL like /about, but the rewrite tells your server something like "act like this is actually ?page="about"), which you then use the $_GET to process.
Being PHP beginner I will not urge you to use .htaccess, As you will need to learn lot many things before you proceed further. You have 2 option to send a request one is GET and POST. You can get more information about same on internet.
Also you have an option to start your dynamic website using CMS and I will recommend you to use wordpress. CMS will have some in-built function which will help you to do your work faster. Also using their control panel you can update the URL format.
I will also urge you to go step by step and follow every tutorial that you will find on internet.
All the best
If you want to do this you have to use .htaccess file and have to load mod_rewrite in your apache server.
In you root directory create file named .htaccess
Then Write:
RewriteEngine On
RewriteRule ^(.*)\.php$ index.php?page=$1 [L,QSA]
And After that call a page
my-page.php
It will redirected as a index.php?page=my-page internally but in browser it will show as my-page.php

Converting site from .html to .php

my site has now become sufficiently large for me to think it's necessary to convert the pages to php pages to help me update it in the future. The problem is: my site has a number of links to it on various websites across the web. Eg these links point to www.example.com/page1.html but the page is now going to be renamed www.example.com/page1.php
How would people get around this problem? Simply redirect the html page to the php page? Are there any alternatives? Does this have any implications for SEO?
Thanks
URL Rewrite: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
More directly to the point: http://roshanbh.com.np/2008/02/hide-php-url-rewriting-htaccess.html
The least intrusive method is to simply have your webserver treat .html files as PHP files. That way your links can stay intact, and progressively replacing static .html pages with actual php-enabled pages can be done in an essentially transparent method to users.
Remember, there is no such thing as a "PHP script". There are only files that contain <?php ... ?> code blocks, which will get interpreted/executed when the containing file is passed through PHP.
Unless some of your html pages contain SAMPLES of php code that could be misinterpreted as actual code, then there shouldn't be any issues with making run through PHP.
As a minor side benefit, it wouldn't be immediately obvious that your site is running on PHP, as all the urls would say ".html". But then, that's security by obscurity and shouldn't be counted on to be anything in the way of real security.
You can do a 301 redirect (this works fine for SEO), or just rewrite the URLs so page1.html points to page1.php internally.
Both solutions can be done with the .htaccess file (assuming you are using apache as your webserver)
Maybe consider using a tool such as Dreamweaver to manage your website. That way you can easily rename pages and update the links with a few clicks.
:)
As answered by Marc B,
there is no such thing as a "PHP script". There are only files that contain code blocks, which will get interpreted/executed when the containing file is passed through PHP.
i would say that it's true and if you want to absolutely turn your html files into php files simply put <?php before your <DOCTYPE html!> line and then ?> after your </html> line save it and rename it as example.php if it is example.html
if you are windows8 or higher user then click on 'View' in file explorer and then check 'File name Extension'. Now you'll be able to see the extension example.html and many other files extensions like .jpg, .mp3 e.t.c...., This helps you to easily rename exactly like example.php but not example.php.html as .html will not be visible if you are not checked File name Extension.
I would suggest that you use CodeIgniter (kickass php framework).
You can maintain the existing site structure also, by making use of CodeIgniter's URL suffix option .

php: newbie question about modrewrite

i've started using modrewrite under php - i wrote an .htaccess file and defined some rules.
but the main question is - how would i do the "opposite thing" of creating the modrewrite-links inside my web?
eg. i'm using this original url inside my website: /search.php?par1=abc&par2=def&par3=ghi
the correspondending modrewrite url would be /go/find/something/here/index.html
so i'd need to replace all html-links in my web using the ugly url with the mod-rewrite url.
what's the easiest way to do this? (function for creating urls, using a database ..)
thanks
As this should be a migration job, only done once, go for search/replace. Using a function or database would mean you have to do the migration for each request to your app. Which seems to be unnecessary.
A function. Simply because that way you can replace what the function does with minimal changes to other parts of the website. This is, for example, the way Drupal does it.

Sensitive information in the document root

Let's say you have a config.php that holds sensitive information like a DB user password. It is not recommended to store that file in the document root, right?
Why is that so and is it a safer approach to store sensitive information in the index.php of the document root?
For me, the first scenario that comes to mind is a misconfiguration that lets users download or view .php files, rather than parse them and present them as text/html. Say you perform an upgrade, something goes wrong, and Apache is no longer parsing your scripts. Somebody notices that Apache is sending your PHP files as plain text, and is able to open config.php and see the source code (and all the sensitive database configuration parameters inside).
To take this idea just a little bit further. Ideally you wouldn't store much more than a simple script that accesses your codebase, and your static files like images and css in the web root.
eg:
webroot/index.php
webroot/images/img1.jpg
webroot/images/img2.jpg
webroot/css/base.css
lib/myclass1.php
lib/myclass2.php
And your index.php would look something like this:
<?php
$CODEBASE = '/usr/home/wwwuser/wherever/it/is';
include $CODEBASE."/lib/myclass1.php";
$code = new MyClass1();
$code->doStuff();
?>
It's not a safer approach to store configuration data in index.php than config.php; they are both equally prone to being displayed if PHP somehow fails to parse their contents. The advantage to using a separate file for configuration data is that you may limit access to it, effectively preventing anyone from reading it over the HTTP protocol and keeping your data safe(r) even if PHP isn't parsing it.

Categories