parameters as part of url instead of after question mark [duplicate] - php

This question already has answers here:
How to create friendly URL in php?
(8 answers)
Closed 6 years ago.
I want to know if we can achieve something like this:
example.com/apps?id='blahblah' converted to example.com/apps/id/blahblah ie. parameters becoming pages themselves. I want to create seperate urls for each id but dynamically. Is this possible in php or any other workaround.

You need to use .htaccess to achieve this.
.htaccess basically rewrites the url to your original location. So to do what you asked in your question it would be like this:
RewriteEngine on
RewriteRule apps/id/(.*?)$ apps?id=$1

You're probably getting downvoted because "mod rewrite in php" has been widely documented with tutorials and articles. Try to search for it here or on Google and you will find plenty of results.
Anyway, if you can't or won't use Apache's mod_rewrite, you can do the same in pure PHP by parsing the HTTP request and process accordingly.
For example
$request_path = $_SERVER['PATH_INFO'];
$request_path = explode("/", $request_path);
// example.com/apps.php/id/blahblah
// $request_path[0] => "example.com"
// $request_path[1] => "apps.php"
// $request_path[2] => "id"
// $request_path[3] => "blahblah"
Notice I added .php extension to apps. If you wish to hide the .php extension you must use mod_rewrite anyway.
From here on you can use the $request_path to figure out what the request wants.

Related

.htaccess redirect to php file [duplicate]

This question already has answers here:
Reference: mod_rewrite, URL rewriting and "pretty links" explained
(5 answers)
Closed 2 years ago.
because .htacces including the syntax are totally new territory for me, I currently don't know how to create a specific redirect. I have given the following link:
www.example.com/RANDOM_STRING
This link should redirect via .htaccess to a PHP file where RANDOM_STRING is saved into a GET Parameter. Roughly speaking, it should be redirected something like this:
www.example.com/RANDOM_STRING > www.example.com/router.php?link=RANDOM_STRING
Is it even possible to redirect something like this via htaccess?
Thx
To redirect /router.php/?link=RANDOM_STRING to /router/ you can try something like the following near the top of your .htaccess file (using mod_rewrite):
RewriteEngine On
RewriteCond %{QUERY_STRING} ^link=
RewriteRule ^link/$ /link/? [R,L]

CodeIgniter URI routing with "#" as path parameter [duplicate]

This question already has answers here:
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
(12 answers)
Closed 2 years ago.
I have a Home.php controller and terms method in it. I have a url like this: https://www.example.com/terms and it works fine.
My routes.php:
$route['default_controller'] = 'home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['terms'] = 'home/terms';
Now, I need to change the url to be like this: https://www.example.com/#/terms Notice the "#" in-between.
I tried adding this to routes but it didn't work:
$route['#/terms'] = 'home/terms';
The above change show home page when I open https://www.example.com/#/terms
However, if I put any other permitted character $config['permitted_uri_chars'] mentioned in conifg.php in place of # then it works fine:
$route['a/terms'] = 'home/terms'; // this works: https://www.example.com/a/terms
$route['2/terms'] = 'home/terms'; //this works: https://www.example.com/2/terms
I also added # in $config['permitted_uri_chars'] but that too didn't work. Keeps showing home page only.
Also, can anyone explain me why # is not treated the same way as other characters in permitted_uri_chars?
Controller is Home.php and method is terms(). Using CodeIgniter version 3.1.11
Thank You!
EDIT:
I need to know if I can achieve it using Codeigniter's routing? Or maybe some rewrite rule using .htaccess?
This is caused by the web browser, not CodeIgniter. Web browsers treat # and anything after it in a URL to be a reference to an element on the web page pointed by the URL before the # part.
So for a URL https://example.com/#page1, the web browser will make a request to https://example.com/ and then look for an element with ID page1 on the web page.
If you want different pages to load by using a '#' in your URLs, it's done using front-end frameworks like Angular. Check Angular's routing features to understand how it makes the browser load a different view for different URI paths after #.

Create better URL - .htaccess [duplicate]

This question already has answers here:
Reference: mod_rewrite, URL rewriting and "pretty links" explained
(5 answers)
Closed 3 years ago.
My URL is looking now like this:
localhost/en/products/bear-toys/bear.php?id=26123
And i need to looks like this:
localhost/en/products/bear-toys/bear/26123
or if i can get title of that product and store into URL:
localhost/en/products/bear-toys/bear/blue-bear.php
I know there is solution with .htaccess but im not good one in .htaccess. So any kind of help will be good. Thanks all
If you want everything after products you can do something like this in htaccess and in PHP. products.php can be the page you want to write to to accept the param(s)
RewriteEngine On
RewriteRule ^(.*)/products/(.*)$ product.php?lang=$1&q=$2 [L,NE]
and this in PHP
$lang = $_GET['lang'];
$params = explode('/', $_GET['q']);
echo $lang . '<br>';
var_dump($params);

how to remove the # in my url [duplicate]

This question already has answers here:
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
(12 answers)
Closed 6 years ago.
https://progressive.e-inkasso.dk/login#application/views/ajax/dashboard.php
when my webpage loads there is this # and not a / i tried to route it to be only dashboard or login in the route.php file but there was no way so far that it worked.
$route['default_controller'] = "Register";
$route['404_override'] = '';
$route['login/(:any)/(:any)/(:any)/(:any)/(:any)'] = "Register/login";
$route['(:any)/dashboard'] = "Register/index/$1";
//$route['(.*)/login'] = "Register/login/$1";
$route['(.*)/register'] = "Register/register/$1";
and i have no clue how to fix it
I think it has something to do with AngularJS and its routing integrated in your website. This is not default behaviour of CodeIgniter. I inspected your page's source code and it seems to load some Angular Javascript things.
Didnt bother to munch to find what and where exactly. But my best guess is that AngularJS is doing this. You should probably redefine your question but i doubt it is possible to remove the trailing #. Good luck

Is it possible to get URL slash/ parameters with simple php?

is it possible to get parameters like:
index.php/bar/foo with php?
I know that I can use s.th. like index.php?a=bar&b=foo and the user $_GET['a']. But I need to do it with the other way.
The answers here just look like guesses. mod_rewrite presumes apache webserver (we don't know which one you use) and is way too much for this simple task.
Apache's default behaviour is to map index.php/xx/xx to index.php?xx/xx.
Look how easy this is:
$args = explode('/', $_SERVER['QUERY_STRING']);
print_r($args);
EDIT: As DanFromGermany pointed out it is actually possible to have urls like index.php/bar/foo without using mod_rewrite or such. I guess the key here is to have a filename (index.php) in the url.
This has to be done in the web server.
In apache there is a module called mod_rewrite which can rewrite urls like index.php/bar/foo to another format such as index.php?a=bar&b=foo.
Examples, and a description of the topic: http://www.seochat.com/c/a/search-engine-optimization-help/creating-search-engine-friendly-urls-with-php/
What web server are you using?
I used following code to access slash parameters:
$args = $_SERVER["REQUEST_URI"];
$arg_arr = explode("/",$args);
print_r($arg_arr);
Hope this helps someone
To achieve the same either you work on httpd.conf for rewriting the URL or use PHP framework like codeigniter etc. which provide inbuilt functionality.

Categories