This question already has answers here:
.htaccess rewrite GET variables
(7 answers)
Closed 4 years ago.
I have a website with a dynamic structure: for every $_GET['action'] I include a file with a different content, while the footer and the header always remain the same. example: www.mysite.com/index.php?action=service1
I would like to change this "structure" in a way:
www.mysite.com/service1/ is there a way to include the $_GET['action'] request in every folder I create?
The site in question has over 100+ $_GET['action'] and creating a page for each action would become heavy .
any advice on how to do?
You can set up your .htaccess to pipe certain values to your $_GET variables.
Learn more here.
The basic idea is to add something like:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^service/(\d+).*$ ./myActionscript.php?id=$1 [L]
Then all requests you send to /service/{number} will go to your file myActionScript and you can do what you want with that {number} there by using string manipulation on the $_SERVER['REQUEST_URI'] to get it.
Related
This question already has answers here:
How to create friendly URL in php?
(8 answers)
URL rewriting with PHP
(5 answers)
Closed 1 year ago.
I am not sure just how to ask this...
www.example.com/
www.example.com/user/list
www.example.com/user/add
How can I make all paths get caught so I can have it go to example.com/index.php and then I can decide what files I want to pull in from the paths...
Instead of doing:
www.example.com/index.php?e=user&a=list
www.example.com/index.php?e=user&a=add
I want something like this:
www.example.com/user/list
www.example.com/user/add
I am not sure if this code will be on a windows server or Unix server. Not sure if I will have .htaccess or not.
you are looking for the .htaccess file !
the .htaccess is a simple file placed in the root directory and allows you to play with the directories and redirect any request to a specific file.
so for example, you can redirect any request for example.com/user/add to example.com/index.php?e=user&a=add
and the user will only type and see example.com/user/add in the address bar. but the actual file that is displayed is example.com/index.php?e=user&a=add
I will take you through the steps needed to achieve that:
in order to display example.com/user/add instead of
example.com/index.php?e=user&a=add first you will need to create a
hidden file which starts with a period and then htaccess and that file should be placed in the main directory
then open that file and write into it the following:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/add index.php?e=user&a=add [NC]
Note:
but that will redirect the exact same url example.com/user/add. if you wanted the user to be a variable so it could be anything such as example.com/hello/add you will need to change your .htaccess content to this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/add index.php?e=$1&a=add [NC,L]
so know {user} is a variable and will be redirected to index.php?e={user}&a=add
This question already has answers here:
CodeIgniter removing index.php from url
(35 answers)
Closed 5 years ago.
My link show this:
http://localhost/project/index.php
I want this:
http://localhost/project/
Do not display extension
You don't understand MVC pattern, there is no direct access to files within URL, everything is redirected to one page.
Full URL: http://localhost/project/home/about
root: http://localhost/project
controller: home
method inside given controller: about
Read more about MVC in Codeigniter's website: https://www.codeigniter.com/user_guide/overview/mvc.html
Or here: https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
CodeIgniter provide Routing rules/URI Routing for this.
You can achive this by adding variable to $route array in
/application/config/routes.php file
$old_path = 'home/about'; // no need to add .php to about
$new_path='home/about'; //or about or any other name you want
$route[$new_path] =$old_path;
Now you can visit page by only
http://localhost/project/home/about
For more details :-
https://www.codeigniter.com/user_guide/general/routing.html?highlight=route
Codeigniter's mvc works as project_folder/index.php/controller/function.
If you modify this behaviour with htaccess it might collapse the behaviour of the framework.
Learn how it works and get clarified. This link might help you.
Create a file with the name ".htaccess" just ".htaccess" NOT "test.htaccess" or so.
Write this code in it ->
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Now you can link your sites like this -> Test
This question already has answers here:
Reference: mod_rewrite, URL rewriting and "pretty links" explained
(5 answers)
Closed 5 years ago.
I'm sorry that my question itself is not the way it should be. I am new to php and the links to open a post ( let's take that I am creating a website something like a blog ) is like open.php?id=sth
I have seen in blogger and many other sites the links to open a post is in the type I have given in the question head. I would like to know how this is done. Is this in a way by which they create a directory and make an html file (as in the example) in that particular directory?
You can do that with a ".htaccess" file and apache url rewrite module.You don't need to create a directory.
Create a ".htaccess" file on the root folder and add something like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^?]*) ./open.php?params=$1 [L]
This will make your url like "http://example.com/2017/name"
You can debug your parameters with that code:
var_dump($_GET);
open.php?url=http://example.com
<?php
header('Location: '+urldecode($_GET['url']);
?>
This question already has answers here:
How to create friendly URL in php?
(8 answers)
Closed 6 years ago.
I couldn't find much help while I was looking for my answer that's the reason for the question on here.
Basically I have this bit of code :
More Info</span>
now how is it possible to get what I am passing ?
Do I make a php file called manage.php ? if so what makes it so that it looks at manage.php ? and then get it through $_GET but how is it possible ?
I am not sure how to go about it so I would appreciate any of yours suggestions.
Thanks
Your problem can have two possible solutions:
By creating manage.php file and passing query string to it.
Your code for the link will like this:
More Info</span>
Now on manage.php page, you can access user_id like this:
<?php $user_id = $_GET["user_id"]; ?>
Using .htaccess, create .htaccess file and put the code below in it. .htaccess file must be placed in same dir where your index.php file is.
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?/$1 [L] </IfModule>
Now you can access your query strings on index.php file using $_SERVER['REQUEST_URI'] global variable.
More Info</span>
Your link to access page must look like above note the / after 10000 else user_id will get merge with 10000
Chatting session was performed for the question, view chat transcript here
You need a routing-engine to manage that type of url.
There are many framework that include routing. The most used is Symfony http://symfony.com/doc/current/routing.html
The general syntax would be as follows:
http://domain_name.com/script.php?parameter1=value¶meter2=value
In your case you would have the following assuming that your parameter is
user_id:
More Info</span>
In PHP you would use $_GET['user_id'] to fetch the passed value
$userID = $_GET['user_id'];
By including these lines in .htaccess
RewriteEngine On
RewriteRule ^manage\/([0-9]+)$ manage.php?user_id=$1
You could reach the same page with the following syntax:
http://domainname/manage/2313
If you mean to say that you need to route to a different php file manage.php and get the passed variable from the previous page then either you can modify the link to
"manage.php?val=10000".$user["user_id"]
and then use $_GET to get the passed variable
Or else you can keep the same url use .htaccess to route your request to manage.php?val=$1
or else in frameworks like codeigniter you have routes file where you can define your route.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want every user request(url) to handle in index.php (or some another file). I need to handle these requests to check if requested page exists(in database) and then construct the page . Using this approach I can store content not in the server itself, but in the database. I suspect that Joomla cms works this way. I am right? Can you post sample code?
(user must see full url he typed in browser's adress bar, and when he click on the backward arrow in browser, he must get the previous page - I suppose i can't use redirect and multiple requests)
There is a design pattern for it called Front Controller Pattern, which provides a centralised entry point.
To achieve this in PHP, you may use any MVC framework like Codeignitor which works in FCP by default.
To achieve this in PHP, first have your .htaccess modified.
RewriteEngine On
RewriteRule . /index.php [L]
And in your index.php, have a logic like this:
index.php (code simplified for understanding)
<?php
if ($_SERVER['REQUEST_URI'] == '/about') {
// Logic for printing About
getContent('about');
} elseif ($_SERVER['REQUEST_URI'] == '/products') {
getContent('products');
} else {
// Logic for printing 404 page
}
function getContent($key) {
// load value based on $key
//print content.
}
Try this in .htaccess
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1 [NC]