htaccess pretty urls setup

前端 未结 2 540
Happy的楠姐
Happy的楠姐 2020-12-10 10:16

I\'m using htaccess for the first time to make pretty urls for my website html files and 1 php file. I was just wondering if I would be able to get some advice on my htacces

2条回答
  •  隐瞒了意图╮
    2020-12-10 11:05

    try adding the following to your .htaccess file in the root of your domain

    Options +FollowSymLinks
    
    RewriteEngine On
    RewriteBase /
    
    # redirect http://www.domain.com/subdomain/htmlpage.html to http://www.domain.com/subdomain/htmlpage/
    RewriteCond %{REQUEST_URI} ^(/subdomain/[^/]+)\.html$ [NC]
    RewriteRule . %1/ [L,R=301]
    
    
    #redirect http://www.domain.com/subdomain/phppage.php?p=1 to http://www.domain.com/subdomain/phppage/1/
    RewriteCond %{REQUEST_URI} ^(/subdomain/[^/]+)\.php$ [NC]
    # or alternatively if page is literally phppage uncomment below and comment above
    #RewriteCond %{REQUEST_URI} ^(/subdomain/phppage)\.php$ [NC]
    RewriteRule . %1/ [L,R=301]
    
    #if url does not end with /
    RewriteCond %{REQUEST_URI} ^(/subdomain/.+[^/])$ [NC]
    # and its not for an existing file
    RewriteCond %{REQUEST_FILENAME} !-f
    # put one trailing slash
    RewriteRule . %1/ [L,R=301]
    
    #write http://www.domain.com/subdomain/htmlpage/ to htmlpage.html
    RewriteCond %{REQUEST_URI} ^/subdomain/([^/]+)/$ [NC]
    RewriteRule . %1.html [L]   
    
    
    #write http://www.domain.com/subdomain/phppage/1/ to phppage.php?p=1
    RewriteCond %{REQUEST_URI} ^/subdomain/[^/]+/([0-9]+)/$ [NC]
    RewriteRule . phppage.php?p=%1 [L]  
    

提交回复
热议问题