I started learning basics of .htaccess file and here i wanted to know how can i create tree stucture url in php using .htaccess file. For example, I have hierarchy like :- science->biology->zoology->dermatology. and domain is www.mydomain.com. So
If I click on science, Url should be www.mydomain.com/science.
Under science If I click on biology, Url should be www.mydomain.com/science/biology.
Under biology If I click on zoology, Url should be www.mydomain.com/science/biology/zoology. and so on.
Here is what I tried so far, created a page called as branch.php, If i click on science i am writing code in branch.php file by using url www.mydomain.com/branch/science and htacces file
Options -Multiviews
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^branch/(.*) branch.php?branch_name=$1 [L]
So I can i create url like www.mydomain.com/science/biology/zoology without creating page(branch.php) using .htacces file.
There is a little disorientation in your thoughts.
Any kind of url you are generating in the PHP without any restrictions.
And .htaccess (and mod_rewrite in particular) used only for redirection of request (transparent for users) to the particular php-file.
So, yes, you can do url like this: http://www.example.com/science/biology/zoology and your .htacces should be like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* branch.php [L]
Then, in the branch.php you should examine $_SERVER['REQUEST_URI'] (it will look like this /science/biology/zoology) and make a decision what to show to the user.
RewriteCond %{REQUEST_FILENAME} !-f is needed to test that requested uri didn't point to real file.
UPDATE: If you want to use $_GET array in the branch.php you can do the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ branch.php?main_branch=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)$ branch.php?main_branch=$1&sub_branch=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ branch.php?main_branch=$1&sub_branch=$2&sub2_branch=$3 [L]
Related
I have a project with the name of social app which contains has a structure like this:
includes
_login.php
_profile.php
pages
login.php
profile.php
currently if I want to vists any page like profile.php I have to visit localhost/social-app/pages/login.php but I want to modify it and change the url to localhost/social-app/login.php. basically I want to get rid of the pages and make my URL a bit cleaner
In your htaccess in the root , put the following :
RewriteEngine on
RewriteRule ^social-app/login\.php$ /social-app/pages/login.php [L]
Now instead of going to long URL you can just type /social-app/login.php to access the file in pages folder.
EDIT :
To remove the directory name for all files , you can use the following :
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/social-app/pages/$1.php -f
RewriteRule ^social-app/([^.]+)\.php$ /social-app/pages/$1.php [L]
If the rule above fails , then use this :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^social-app/(.+)\.php$ /social-app/pages/$1.php [L]
This will internally map a request for /social-app/filename.php to /social-app/pages/filename.php .
If everything (ie. images, CSS, JS and PHP pages) need to be rewritten to the /pages subdirectory then you can do something like the following in the /social-app/.htaccess file using mod_rewrite:
RewriteEngine On
RewriteRule !^pages/ pages%{REQUEST_URI} [L]
This unconditionally rewrites everything that is not already prefixed with pages/ to the pages subdirectory. eg. /social-app/login.php is internally rewritten to /social-app/pages/login.php.
If you have static resources in other locations that should not be rewritten then include a filesystem check to prevent requests that already map to existing files from being rewritten. For example:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^pages/ pages%{REQUEST_URI} [L]
If you are changing an existing URL structure then you'll need to redirect the old URLs that are indexed by search engines and perhaps linked to from third parties. For example, the following "redirect" would need to go before the above rewrite:
RewriteBase /social-app
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^page/(.*) $1 [R=301,L]
i want to redirect users to my site based on the url they typed into the address bar but i am problems as what to actually do. forgive me if i am not using the right term, i am a newbie.
this an example of what i want to achieve
www.mysite.com (is my site)
www.mysite.com/pizza (is what the user typed)
i want the browser to take the user to www.mysite.com if he types www.mysite.com and redirect the user to www.mysite.com/base/tweek.php?ref=pizza if he types www.mysite.com/pizza.
how do i do this?
Try adding this to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /base/tweek.php?ref=$1 [L]
Also make sure you have mod_rewrite loaded.
You must create the .htaccess file in the root directory of the page, and write:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /base/tweek.php?ref=$1 [L,QSA]
I think (handwritten, not checked :) ).
Hope it helps, if You have any problems, try to read some htaccess tutorial.
Just use header at the top from your index.php:
header('Location: anotherindex.php');
Let say I have a site
www.mysite.com
Is there anyway to have to have user page urls be
www.mysite.com/username
instead of the usual
www.mysite.com/user.php?id=whatever
I would like to know if this is possible in php without having multiple folders and index pages. I would like to avoid the folder method because from my understanding you would have an issue with efficiency especially if you have to bounce from one user to another. I would like to do this method because it is a lot easier for someone to say get my info from
www.mysite.com/username
than the other option. Any help (tutorials, sites, etc) would be appreciated. Thanks in advance.
I would like to know if this is possible in php without having multiple folders and index pages.
Yes, its possible. You can do it redirecting all requests to your index.php and process this url (route) manually
sample .htaccess for redirecting all request to your index.php
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?route=$1 [L,QSA]
BUT implemention of
www.mysite.com/username
is not easy thing. You have to know all your system usrls such as /register, /login, /post, ... etc and dont allow to register these usernames.
I recomend you implement this scheme:
www.mysite.com/~username
or
www.mysite.com/user/username
Apply front controller pattern with any kind of router [e.g. klein], then you'll need a single, universal .htaccess file in your webroot:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
Yes sure, use this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /user.php?id=$1 [L,QSA]
At the root of my site... www.domain.com . want to add some static pages that the page url can be set from the user.
So if the users set as url profile then full page url should be www.domain.com/profile ..
So far a simple rewrite rule would do the job.
trasnlate it to something like /staticpage.php?tag=profile
The problem that i want some pages like www.domain.com/shop at the root which arent static...
So what can i do if all the requests for the main directory go to /staticpage.php?tag=$1 ?
I recommend using mod rewrite to send everything to your index.php file and using a front controller to do this. It makes it much easier.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
You'll find a lot more help about mod_rewrite on ServerFault as a general rule, but I tend to do this:
RewriteEngine on
RewriteRule ^static.*$ - [L]
RewriteRule ^assets.*$ - [L]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule .* /router.php
where "static" are uploaded files, and "assets" are production graphics/stylesheets/js libraries etc.
Every single time a user registers on my site I would like them to have their own subdirectory with their registered "username". Every user subdirectory will have the same "index.php" file which will do something.
For example: "/users/username1/" and "/users/username2/"
If some one wants to access the subdirectory they would simple go to:
"www.example.com/users/username1/" or "www.example.com/users/username2/"
The easy and messy solution would be to simply create a subdirectory for every user and place the same "index.php" file in every directory. But to me this is only going to crowd my server space and make my directories large.
I wanted to know if all this can be done using .htaccess? Can I create one "index.php" and one ".htaccess" file and place them both in my "/users/" directory? What would be the actual code that I would have to place in my .htaccess file??
If you have a better way of doing this please let me know. I am using Apache and PHP as my working environment.
Thank you
Well, for example, you could do it all with one htaccess like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
What it does:
switches on rewrite engine
checks if a requested file exists
checks if a requested directory exists
if NOT, it redirects request to your main index.php
Basically that means if you enter url such as yourdomain.com/users/ivan/, you request will be redirected to:
index.php?url=/users/ivan
then you $_GET['url'] in your index.php and split it into pieces.
That's just an example, there other mod_rewrite methods to do this.
Make it virtual. There are no subdirectories, you can use mod_rewrite to simulate that.
With mod_rewrite you can make /users/username1 lead to /users.php?user=username1 for instance. Everything is transparent for the client, he wont notice what is really happening.
By using something like this:
RewriteEngine On
RewriteRule ^([\-_0-9A-Za-z]+)$ index.php?a=$1 [L]
You can customize RewriteRule as much as you want.
You can essentially type in any directory you want, and it will be redirected to your index.php page.
If you want to make sure the existing directories are not redirected, do this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\-_0-9A-Za-z]+)$ index.php?a=$1 [L]
If you want to limit the scope, so only a subdirectory of user/ is redirected (similar to Stack Overflow), simply add in 'user' to the start of the rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^user/([\-_0-9A-Za-z]+)$ index.php?a=$1 [L]
And finally, if you want to have an individual file handle all user requests seperate from your actual index.php page:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^user/([\-_0-9A-Za-z]+)$ users.php?a=$1 [L]
This is a very similar setup I use to distribute CSS files.
Note: The Directory will be contained is $_GET['a']