How to make long url show just file name with htaccess

Hello!
I m really new in htaccess and was fighting with this few hours so i finally came to post a question !

i have this url page_cars?view=data&section=cars&year=2017&type=new

how to rewrite this and all other pages with once htaccess code so just php file name is left in browser url?

example
www.mydomain.com/page_cars

Big thanks for answers :slight_smile:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1.php?view=$2&section=$3&year=$4&object=$5 [L]



Options -Indexes

Try a Google search for “.htaccess pretty urls”

I m not that lazy. …What you thing i did that few hours ?Tried to make new world?

Ok, no problem.

Can you show the script which is not working.

Code added!

Try this:
file: “.htaccess”

RewriteEngine on
RewriteCond %{REQUEST_FILENAME}/$1 !-d  
RewriteCond %{REQUEST_FILENAME}/$1/ !-f
RewriteRule (.*) page_cars.php?view=$1&section=$2&year=$3&object=$4 [L]

file: “page_cars.php”

<?php 

declare(strict_types=1); // PHP 7 specific

error_reporting(-1);
ini_set('display_errors','true');

$lftab = '<br> &nbsp; &nbsp; &nbsp;  ';
$link  = '?/page_cars?view=data&section=cars&year=2017&type=new';

echo "<a href='$link'> $link </a>";
  echo '<br><br>';

echo 'this __FILE__ ' .$lftab .__FILE__;
  echo '<br><br>';

echo 'REQUEST_URI ' .$lftab .$_SERVER['REQUEST_URI'];
  echo '<br><br>';

echo 'REDIRECT_QUERY_STRING '  .$lftab .$_SERVER['REDIRECT_QUERY_STRING'];

#echo '<br><br><br><br>';
# echo '<pre>'; print_r( $_SERVER ); echo '<pre>';
die;

declare(strict_types=1); // PHP 7 specific works only for php 7 and higher ?

If you are using PHP 7.X then this is very strict and shows more errors.

If you are using PHP 5.X then a warning will be produced because declare(…)l is not implemented.

REM the line to prevent the warning.

<?php
 # declare(strict_types=1); // PHP 7 specific

 echo phpversion();  // will show your PHP version;
 exit;;
  // I am using - 7.0.15-0ubuntu0.16.04.4
 

I’m confused. The parentheses in the pattern are a single capture. So at the most you can only have a “1”

If the GET is something like
example.com?foo=one&bar=two&baz=three
You could do like
'example.com/one/two/three`
capture the “folder” values and use them.

But if you want to have the HTTP URL 'example.com` load different content depending on variables, they will need to be passed along somehow. If not as GETs that look like folders, maybe POST, SESSION, COOKIE, localStorage etc. is what you’re looking for?

I need my link end just with php file name :slight_smile:
example
www.example.com/page_cars.php?view=data&section=cars&year=2017&type=new
so it becomes
www.example.com/page_cars
and works the same as long url!

Is that even possible?And how hard it could be?

I live in the United States of America, send me a letter and I’ll let you know.

See the problem?

It would be possible to have a page load with default values. But other than that the values need to be passed along somehow.

This one shows me just empty page with my header :frowning:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.