Apache URL Re-writing not functioning properly

前端 未结 12 1478
春和景丽
春和景丽 2021-01-17 21:31

I am trying to use apache-rewrite rule to convert the below URL:

http://localhost/foo/bar/news.php?id=24

Into this

12条回答
  •  伪装坚强ぢ
    2021-01-17 22:08

    If you feel your .htaccess file is not working as intended then this is a server configuration issue and most likely to do with the AllowOverride directive under the Apache configuration.

    In your http.conf file find the section which looks something as follows:

    
        Options FollowSymLinks
        AllowOverride None
    
    

    Change the AllowOverride directive to allow All.

    
        Options FollowSymLinks
        AllowOverride All
    
    

    The next thing is to ensure that the mod_rewrite module is enabled for your XAMPP install. Search for the following line:

    #LoadModule rewrite_module modules/mod_rewrite.so
    

    Remove the # so it looks like so:

    LoadModule rewrite_module modules/mod_rewrite.so
    

    Restart the Apache service after saving all your changes.

    Also ensure you are using the RewriteBase directive in your .htaccess configuration as follows:

    RewriteEngine On
    RewriteBase /DIRECTORY/AID/
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-l
    ^news/([A-Za-z0-9_]+)(/)?$ news.php?title=$1 [QSA,L]
    

    The next thing is to ensure your links are pointing to the Rewrite URL. Your echo line should be something like the following:

    echo "  " . substr($row['title'], 0, 26) . "...
    ";

    Now since you can only retrieve titles with this URL rewrite method we need to configure your PHP script accordingly to retrieve the content based on the title. If you still want to use "id" only for retrieving the record then your Rewrite URL should contain the "id" in it in some form. Typical examples of this form are:

    • news/the_news_title_123
    • news/123_the_news_title
    • news/123/the_news_title

提交回复
热议问题