i need in naming like this:
site.com/about
site.com/contacts
Could i do it without .htaccess?
If you want a very simple implementation then you could structure your folders to allow you to do something like:
site.com/about/ - which will go to /about/index.php
site.com/contacts/ - which will go to /contacts/index.php
But obviously there's no room for any dynamic URLs, for that you would need a .htaccess implementation. They're very simple to do.
If you mean "Can I configure my webserver to parse /about and /contacts as PHP without using an .htaccess file", then no. Without further configuration (i.e. a local htaccess, or the global configuration files), Apache will not pass them through the PHP handler. You also won't be able to setup these URLs are redirects (internal or otherwise) without configuration (again via htaccess).
You could do it without a .htaccess file if you had access to the apache config files that define your site. However, if you can't use .htaccess files, you likely don't have access to the apache configs either. Might be wroth asking your host / sysadmin.
You might also be able to do it through the 404 handler if your host lets you have access to that.
that is rewriting
google search
similar to:
Options +FollowSymLinks
RewriteEngine on
Rewriterule ^about /menu/about.html
Rewriterule ^contacts /menu/contacts.html
Related
I am trying to find out how websites like Imgur, MEGA and such are able to do this:
https://imgur.com/a/SbmNz (emphasis on SbmNz)
The SbmNz bit is dynamic between images or files and I guess it is a kind of $_GET. And I was just wondering how you can do this without the usual ?name=value way.
you can use any MVC or just write a htaccess rule for it , for instance, you can use laravel to pass variables along with url
for laravel refer URL Generation-Laravel5
to write .htaccess refer USING .HTACCESS REWRITE RULES
You can do with Apache rewriting module by changing .htaccess (Create .htaccess file in your folder)
For example if you have this snippet
RewriteRule ^play/([^/]*)$ player.php?id=$1 [L]
When user visits www.example.com/play/Trg4 in backend request is actually handled for www.example.com/player.php?id=Trg4
Then you can get the id with $_GET['id']. it means there is no change with php code. it is completely done with .htaccess
Make sure you enabled rewrite_module of your Apache server
I'm very new to php and web , now I'm learning about oop in php and how to divide my program into classes each in .php file. before now all I know about php program, that I may have these files into my root folder
home.php
about.php
products.php
contact.php
So, whenever the client requests any of that in the browser
http://www.example.com/home.php
http://www.example.com/about.php
http://www.example.com/products.php
http://www.example.com/contact.php
No problem, the files will output the proper page to the client.
Now, I have a problem. I also have files like these in the root folder
class1.php
class2.php
resources/myFunctions.php
resources/otherFunctions.php
how to prevent the user from requesting these files by typing something like this in the browser ?
http://www.example.com/resources/myFunctions.php
The ways that I have been thinking of is by adding this line on top of every file of them exit;
Or, I know there is something called .htaccess that is an Apache configuration file that effect the way that the Apache works.
What do real life applications do to solve this problem ?
You would indeed use whatever server side configuration options are available to you.
Depending on how your hosting is set up you could either modify the include path for PHP (http://php.net/manual/en/ini.core.php#ini.include-path) or restricting the various documents/directories to specific hosts/subnets/no access in the Apache site configuration (https://httpd.apache.org/docs/2.4/howto/access.html).
If you are on shared hosting, this level of lock down isn't usually possible, so you are stuck with using the Apache rewrite rules using a combination of a easy to handle file naming convention (ie, classFoo.inc.php and classBar.inc.php), the .htaccess file and using the FilesMatch directive to block access to *.inc.php - http://www.askapache.com/htaccess/using-filesmatch-and-files-in-htaccess/
FWIW all else being equal the Apache foundation says it is better/more efficient to do it in server side config vs. using .htaccess IF that option is available to you.
A real-life application often uses a so-called public/ or webroot/ folder in the root of the project where all files to be requested over the web reside in.
This .htaccess file then forwards all HTTP requests to this folder with internal URL rewrites like the following:
RewriteRule ^$ webroot/ [L] # match either nothing (www.mydomain.com)
RewriteRule ^(.*)$ webroot/$1 [L] # or anything else (www.mydomain.com/home.php)
.htaccess uses regular expressions to match the request URI (everything in the URL after the hostname) and prepends that with webroot/, in this example.
www.mydomain.com/home.php becomes www.mydomain.com/webroot/home.php,
www.mydomain.com/folder/file.php becomes www.mydomain.com/webroot/folder/file.php
Note: this will not be visible in the url in the browser.
When configured properly, all files that are placed outside of this folder can not be accessed by a regular HTTP request. Your application however (your php scripts), can still access those private files, because PHP runs on your server, so it has filesystem access to those files.
I'm developing some web based application based on PHP.
I have some folder structure that will be located inside the public html file.
I'd like to make it work so that when a user types for ex. http://mysite.com/ he/she gets into http://mysite.com/public but I don't want the user to know that he/she is inside public, the user should think that his directly inside public_html folder.
Any hints?
P.S. I'm doing it on hosted server, so I have access with only Cpanel, I'm not the admin of the server.
You either need to use mod_alias or mod_rewrite for this. How much of cPanel is available to you? How much does you host let you do?
I'll just have to have a look through my WHM server to work out how to do Aliases, but you can do rewrites with a .htaccess file. I would recommend Aliases over rewrites thought, as they are less complicated and less resource-hungry.
EDIT
Just been into my root login for our WHM/cPanel based server, and I can't find any way to use mod_alias - I think this is probably because it would require an Apache restart. You will have to use mod_rewrite.
Put this in a .htaccess file in public_html:
RewriteEngine on
RewriteRule (.*) public/$1 [L]
You can set up an addon domain and point it to /public_html/public directory.
Edit:
Parked domain should work too.
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.. ...
What is the best way to dynamically redirect a friendly looking url on Linux to dynamic on Windows?
ex.
domainone.com/dir/one/two
redirects to
domaintwo.com/index.aspx?a=one&b=two
and
domainone.com/dir/three/four
redirects to
domaintwo.com/index.aspx?a=three&b=four
The HTTP header called Location is how you redirect a user across hosts/domains.
Depending on your server configuration and the mechanism used to generate the HTTP headers, the specific implementation will vary. An example in PHP (as your question appears to be tagged) is to include the following code:
header('Location: http://domaintwo.com/index.aspx?a=one&b=two');
The string above is like any other string, so apply the appropriate logic to provide the desired redirection URL.
The same effect is also possible in the domain configuration files (the precise path differs across server software and operating system) or more conventionally in .htaccess files. If you provide more information about your hosting environment, someone will be able to help you devise the rewrite rule you need. I prefer to put this level of smart rewriting in a PHP script, since I think .htaccess files tend to be harder to manage and "read".
From within Apache:
Either in a server configuration file, or more likely in an .htaccess file.
You can use mod_rewrite to do this, but as you want a redirect, it would be more appropriate to use mod_alias and the RedirectMatch statement.
RedirectMatch 301 ^/a/([^/]+)/([^/]+)$ http://domaintwo.com/index.aspx?a=$1&b=$2
Rewrite variant:
RewriteEngine On
RewriteBase /
RewriteRule ^/a/([^/]+)/([^/]+)$ http://domaintwo.com/index.aspx?a=$1&b=$2 [R=301,L]
Note the use of 301, that is a permanent redirect, use 302 for temporary, or when you always want people to redirect rather than going directly on future accesses.
A pretty standard way to approach this on Linux is to use Apache with mod_rewrite (how to install and configure Apache and mod_rewrite, if it's not already set up, will depend on your Linux distribution)
Then, in your Apache configuration, you can add a line like:
RewriteEngine On
RewriteRule ^/a/([^/]+)/([^/]+) http://www.domaintwo.com/index.aspx?a=$1&b=$2