I have a URL https://www.localhost/sitpoint/cellphone?id=Apple
I want to convert cellphone?id=Apple
into to /Apple
like https://www.localhost/sitpoint/Apple
using .htaccess url rewrite code.
Asked
Active
Viewed 131 times
-2

Muhammad Riaz
- 403
- 3
- 8
- 23
-
Sounds great. Consider using mod_rewrite. – drmarvelous Oct 01 '15 at 17:39
1 Answers
1
I would like to be able to know more about what you want to accomplish, you can't simply do it easily, and you have to consider a lot of other factors, but here is a code you can use to let Apache know that whatever goes inside the first URL parameter, goes as a value for a variable::
Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?id=$1 [QSA,L]
In your index.php file: (or any other file you're redirecting to)
if(isset($_GET["id"])) {
//Split URL by '/' character, putting each one inside an array
$_URL = explode("/", filter_var(rtrim($_GET["id"], "/"), FILTER_SANITIZE_URL));
$id = $_URL[0];
//if user inputs www.myaapp.com/Apple
//$id would be = 'Apple'
}

Juan Bonnett
- 1,823
- 1
- 15
- 33