I'm trying to rewrite user/John-Doe to user?name=John-Doe with mod_rewrite
in .htaccess
. I have the .htaccess in the same directory with the file I'm rewriting/redirecting. This is the code in the htaccess:
RewriteEngine on
RewriteRule ^user/(\w+)/?$ user?name=$1
Now I do not receive any data from $_GET['name'] and var_dump($_GET) is empty.
You need to capture more characters with [\w-]+
Also, you are point the request to user?name=$1
that should be user.php?name=$1
?
.htaccess
<IfModule mod_rewrite.c> # Check if mod_rewrite if avaible
RewriteEngine on
RewriteRule ^user/([\w-]+)/?$ user.php?name=$1
</IfModule>
user.php
<?php
var_dump($_GET);
array(1) { ["name"]=> string(8) "John-Doe" }
Test example
https://regex101.com/r/up32vA/1
Also check if mod_rewrite
is enabled in your server, a easy way is putting this in index.php
print_r( apache_get_modules() );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.