How to create clean url using .htaccess

前端 未结 3 869
栀梦
栀梦 2020-12-01 23:05

This is my .htaaccess code.

RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/$ movie.php?name=$1
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example\\.in$
Rew         


        
相关标签:
3条回答
  • 2020-12-01 23:24
    RewriteEngine on
    RewriteRule ^movie/([0-9a-zA-Z]+)$ movie.php?name=$1 [L]
    

    Add above code in .htaccess file

    0 讨论(0)
  • 2020-12-01 23:34

    If you're want to have clean URLs, then you have to use clean URLs in your html code, e.g. use

    <a href="/movie/titanic">Titanic</a>
    

    Your rewrite rules will take that doesn't-really-exist-on-the-server address (you don't really have a /movie directory that contains a titanic file, after all), and translates it in to the actual

    /movie.php?name=titanic
    

    The user would never see this url, however, because the rewrite would take place purely within Apache. As long as the HTML you send to the user contains only "clean" urls, they'll never see the query strings.

    0 讨论(0)
  • 2020-12-01 23:42

    Replace your code with this:

    ErrorDocument 404 /404.php
    AddDefaultCharset UTF-8
    Header unset ETag
    FileETag None
    
    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+movie\.php\?name=([^\s&]+) [NC]
    RewriteRule ^ movie/%1? [R=301,L]
    
    RewriteRule ^movie/([^/]+)/?$ movie.php?name=$1 [L,QSA]
    
    0 讨论(0)
提交回复
热议问题