I am trying to rewrite the URL of my website for PHP page I have different conditions
first example.com/view.php?id=1
Here I need to hide id from the URL I need it like this
example.com/view/1
second, for some page, I have different variable like this example
example.com/view/book.php?id=1&user=33333&booked=1
I need to rewrite link to this example
example.com/view/book/1/33333/1
here is what I did in .htaccess page
# Remove .php file extension on requests
RewriteRule ^(. ).php$ /$1 [R,L]
RewriteRule ^(view/booked)/([0-9]*)$ $1.php?id=$2 [END]
# Append .php extension for other requests
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ /$1.php [END]
RewriteRule ^(view/book)/([0-9]*)/([0-9]*)/([0-9]*)$ $1.php?id=$2&user=$3&booked=$4 [END]
for example 1 it works well example.com/view/1 without any problem and for example.com/view/booked/1 as well.
The only problem with the second example example.com/view/book.php?id=1&user=33333&booked=1 I need to hide variables
CodePudding user response:
With your shown samples please try following .htaccess rules.
Please make sure to:
- Clear your browser cache before testing your URLs.
- Please make sure your .htaccess file is residing along with
view
folder ANDview.php
in same root folder.
RewriteEngine ON
##Redirect from example.com/view.php?id=1 To example.com/view/1 rules.
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php\?id=(\d )\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Internal handling to rewrite to view.php file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ $1.php?id=$2 [QSA,L]
##Redirect from example.com/view/book.php?id=1&user=33333&booked=1 TO example.com/view/book/1/33333/1
RewriteCond %{THE_REQUEST} \s/(view)/([^.]*)\.php\?id=(\d )&user=(\d )&booked=(\d )\s [NC]
RewriteRule ^ /%1/%2/%3/%4/%5? [R=301,L]
##Internal handling to rewrite to book.php file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)?$ $1/$2.php?id=$3&user=$4&booked=$5 [QSA,L]
CodePudding user response:
I recommend to keep things clear and precise. That probably is what you are looking for:
RewriteEngine on
# redirect /view/book.php?id=1&user=33333&booked=1 => /view/book/1/33333/1
RewriteCond %{QUERY_STRING} ^id=(\d )&user(\d )&booked=(\d )$
RewriteRule ^/?view/book\.php$ /view/book/%1/%2/%3 [R=301,END]
# redirect /view.php?id=1 => /view/1
RewriteCond %{QUERY_STRING} ^id=(\d )$
RewriteRule ^/?view\.php$ /view/%1 [R=301,END]
# rewrite /view/1 => /view.php?id=1
RewriteRule ^/?view/(\d )$ /view.php?id=$1 [END]
# rewrite /view/book/1/33333/1 => /view/book.php?id=1&user=33333&booked=1
RewriteRule ^/?view/book/(\d )/(\d )/(\d )$ /view/book.php?id=$1&user=$2&booked=$3 [END]
# rewrite to php files, if those exist
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_FILENAME}.php [END]
It probably makes sense to start using R=302
temporary redirections first. And to only change those to R=301
permanent redirections once everything works as desired. That prevents nasty caching issues on the client side. Also you always want to test using a fresh anonymous browser window.
Best place to implement such rules is in the central http server's host configuration. If you do not have access to that you can use a distributed configuration file instead (".htaccess"), if you have enabled the consideration of such files in the http server ... Above rules will work likewise if that file is placed inside the DOCUMENT_ROOT
folder of the http host.