Rewriting / redirecting Apache url with database data

Here is my PHP code (qs.php). This file contains pretty url links.

<html>
<head></head>
<body>
    <?php  
    $file = 'qs1'; //this is a php file qs1.php
    $id = '12345678'; //testing ID
    $complete_url = $file."/".$id;

    ?>
    <a href ="<?php echo $complete_url;?>"> This is a test link </a>

</body></html>

This link shows the link - http://localhost/qs1/12345678

QS1 is a php file (qs1.php).

Below is the htaccess file.

RewriteEngine On
RewriteBase /

RewriteRule ^([^/]+)/(\d+)/([^/]+)$ $qs1.php?var1=$2 [NC,L]

In the qs1.php file, I get a query string var1on$_GET['var1']

I want the link to be accessible using this URL http://localhost/qs1/12345678. and if the user puts a slash at the end, like this one http://localhost/qs1/12345678/, then the page redirects itself to this one http://localhost/qs1/12345678.

I am currently getting 404 error opening this http://localhost/qs1/12345678/

Thank you for your comment.

0
source share
1 answer

, - http://example.com/qs1/12345678/something

htaccess

Options -MultiViews

RewriteEngine On
RewriteBase /

# we don't touch existing files/folders
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# remove trailing slash (for instance: redirect /qs1/12345678/ to /qs1/12345678)
RewriteRule ^([^/]+)/(\d+)/$ $1/$2 [R=301,L]

# rewrite /xxx/12345 to /xxx.php?var1=12345 (if xxx.php exists)
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^([^/]+)/(\d+)$ $1.php?var1=$2 [L]

# rewrite /xxx/12345/something to /xxx.php?var1=12345&var2=something (if xxx.php exists)
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^([^/]+)/(\d+)/([^/]+)$ $1.php?var1=$2&var2=$3 [L]
+3

All Articles