简体   繁体   中英

Issues with .htaccess file to make SEO Friendly URLs

i'm new to web development and i'm trying make SEO friendly URLs using .htaccess file at the root of my directory. I've modified my apache2.conf file so that it includes :

<Directory /home/sakyun/sites/> (Being my file directory that i changed)
    Options Indexes FollowSymLinks
    AllowOverride All (So that my .htaccess file is read)
    Require all granted
</Directory>

I've enabdle rewrite mod with:

sudo a2enmod rewrite

I've restarted apache with:

sudo service apache2 restart

My .htaccess is at the root of my directory and contains the following :

# Turn Rewrite Engine On
RewriteEngine on
# Rewrite for profile.php?Id=1&Name=Name
RewriteRule ^profile/([0-9]+)/([0-9a-zA-Z_-]+)$ php/profile.php?Id=$1&Name=$2 [NC,L]

So to me it seems that everything is correct but when i load my page my url is still like this :

localhost/abc/php/profile.php?Id=1&Name=Name

Where i'm expecting something more like :

localhost/abc/profile/1/Name

Also i'm running UBUNTU 16.04 In a Virtual Machine not sure if that helps.
What am i doing wrong?
Is apache even taking my .htaccess file into consideration?
If not what do i do to get this to work?

The following RewriteRule only rewrite the URL internally.

RewriteRule ^/?abc/profile/([0-9]+)/([0-9a-zA-Z_-]+)$ /abc/php/profile.php?Id=$1&Name=$2 [NC,L]

When users requested localhost/abc/profile/1/Name , the url browser shown is localhost/abc/profile/1/Name and they are actually viewing the page localhost/abc/php/profile.php?Id=1&Name=Name .

You have to add another RewriteRule to redirect user to SEO friendly URL.

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/abc/php/profile.php [NC]
RewriteCond %{QUERY_STRING} Id=([^&]+)&Name=([^&]+)
RewriteRule ^/?abc/php/profile.php$ /abc/profile/%1/%2? [R=301,L]

It checks if users are requesting the URL localhost/abc/php/profile.php , and if containing query string Id and Name , redirect the users to SEO friendly URL. %1 is backreference to first ([^&]+) and %2 is backreference to the second one ([^&]+) , which matches all the characters except & .

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