Create better URL - .htaccess [duplicate] - php

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);

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]

Rewrite URL to MySQL value [duplicate]

This question already has answers here:
How can I create friendly URLs with .htaccess?
(8 answers)
Closed 8 years ago.
I have a PHP script that queries the database with the id specified in the URL:
http://www.example.com/articles.php?id=5
But I want the URL not to have the id in it but the title that's in the same database in another column. So I basically need to have a link that looks like this:
http://www.example.com/articles/title-of-the-fifth-article
and search for this title in the database so I can rewrite the URL in the .htacces file to where it specifies the id.
This should work:
Add this to the .htaccess file
RewriteEngine On
RewriteRule ^articles/(.*) articles.php?title=$1 [L]
and the following to the articles.php file:
<?php
echo $_GET['title'];
?>

I need to hide .php file name in url [duplicate]

This question already has answers here:
How to make .php extension not appear on website? [duplicate]
(2 answers)
Closed 9 years ago.
I want to hide the file name with the extinction of .php.I tried to write .htaccess file to restrict my URL.but it is not hiding. any one can help me step by step how to hide the URL.
bellow I mentioned actual URL.and expected URL.
Actual URL : varthakindia.com/inners.php?cid1=Hotels
Expected URL: varthakindia.com/Hotels/
In .htaccess
RewriteEngine On
RewriteRule ^Hotels$ inners.php?cid1=Hotels [L,NS]
Use mod_rewrite
RewriteEngine on
RewriteRule ^Hotels/?$ inners.php?cid1=Hotels [L, NS]

How to generate seo friendly URLs use .htaccess with PHP? [duplicate]

This question already has answers here:
Closed 10 years ago.
I use GET in index.php:
$request=isset($_GET['m']) && $_GET['m']!='';
$requestFile=isset($_GET['files']) && $_GET['files']!='';
My link is in the format: localhost:81/Template/index.php?m=index&files=Acc.
How do I use a .htaccess file or PHP to rewrite to: localhost:81/Template/Acc/index.html or php?
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^Template/([^/]+)/([^/.]+)\.html$ /index.php?m=$2&files=$1 [L]
To obtain your page from:
localhost:81/Template/Acc/index.html

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

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.

Categories