问题
Below is my php code(qs.php). This file contain url links. Pretty links created by using SLUG.
<html>
<head></head>
<body>
<?php
function create_slug($string){
$slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
$slug = strtolower($slug);
return $slug;
}
$slugurl= create_slug('SLUG-text-testing-purpose-only');
$file = 'qs1'; //this is a php file qs1.php
$id = '12345678'; //testing ID
$complete_url = $file."/".$id."/".$slugurl;
?>
<a href ="<?php echo $full;?>"> This is a test link created by slug</a>
</body></html>
Above link appear like this - http://localhost/qs/qs1/12345678/slug-text-testing-purpose-only
Edit
QS is a directory where all files are placed. QS1 is a php file (qs1.php)
I want to get two variable from above link 12345678 & slug-text-testing-purpose-only
.
as qs1.php is file where i get these two variables from $_GET['var1'] & $_GET['var2'];
I know it can be achieved by .htaccess
. i tried multiple options but they are not working as i am getting 404.
.htaccess file is placed on QS directory.
Below are the options i had tried.
RewriteEngine On
RewriteRule ^qs/([0-9]+)/([^/]+)$ qs1.php?var1=$1&var2=$2 [L]
RewriteRule ^/([^/]*)/([^/]*)$ /qs1.php?var1=$1&var2=$2 [L]
RewriteRule ^qs/([^/]+)/([^/]+)/?$ /qs1.php?var1=$1&var2=$2 [QSA,L]
RewriteRule /qs/(\d+)/(.*) qs1.php?id=$1&slug=$2 [QSA,L]
I am very new to htaccess and url redirect.
Please advise.
Thanks
回答1:
You can put this code in /qs/.htaccess
RewriteEngine On
RewriteBase /qs/
RewriteRule ^([^/]+)/(\d+)/([^/]+)$ $1.php?var1=$2&var2=$3 [NC,L]
Now, you can go to http://localhost/qs/qs1/12345678/slug-text-testing-purpose-only
and you'll get the same content as /qs/qs1.php?var1=12345678&var2=slug-text-testing-purpose-only
回答2:
RewriteEngine On
RewriteRule ^qs/qs1/(\d+)/(.+)$ qs1.php?var1=$1&var2=$2 [NC,L]
You can play with it here: http://htaccess.madewithlove.be
来源:https://stackoverflow.com/questions/30280098/url-rewriteslug-and-url-redirection-htaccess