简体   繁体   中英

How can I redirect correctly with .htaccess?

At this moment I'm working on a project for a website. I only have an issue. My .htaccess won't redirect correctly.

On my project users can register and there is an members list. The members list is sorted by A till Z and you can select it on the page like this: Click here when you click on a letter it will show you the users that are registered on that specific letter

The problem is that it won't redirect like i have it in my .htaccess

I tried many things and tried to search it but i can't find any fix

This is what the .htaccess looks like:

RewriteRule ^memberlist memberlist.php

RewriteRule ^memberlist/show/(.*) memberlist.php?show=$1

and this is in the PHP file

echo $content_1;
for($letter = 0; $letter <= 13; $letter++) {
    echo "- <a href='/memberlist/show/".$array[$letter]."'>".$array[$letter]."</a> \n";
}
echo "-<br> \n";
for($letter = 14; $letter <= 25; $letter++) {
    echo "- <a href='/memberlist/show/".$array[$letter]."'>".$array[$letter]."</a> \n";
}
echo "-<br> \n";
for($cijfer = 1; $cijfer <= 9; $cijfer++) {
    echo "- <a href='/memberlist/show/".$cijfer."'>".$cijfer."</a> \n";
}

As you can see in the code i want to redirect the users to the specific letter like this: mydomain.com/memberslist/show/A but it won't work and if i do mydomain.com/memberslist.php?show=A it will work.

Sorry for my bad english (I'm dutch)

The rule RewriteRule ^memberlist memberlist.php will match even for mydomain.com/memberlist/show/A because the regex ^memberlist only checks if the path starts with memberlist .

Instead write the first rule as:

RewriteRule ^memberlist$ memberlist.php

The $ signifies that after memberlist the string has to end. That means it will only match mydomain.com/memberlist

To add, you can easily test your rules with this site: https://htaccess.madewithlove.be/
It will show you which rules match a given url. Remember that the first matched rule will be chosen. That means another solution would just be to reverse your rules so that the more specific one comes first.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM