强制https时删除www和html

时间:2017-09-19 21:09:03

标签: html .htaccess url-rewriting url-redirection

如何从我的网址中删除www并强制https并同时删除.html?

RewriteEngine on

# force ssl
RewriteCond     %{SERVER_PORT} ^80$
RewriteRule     ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

# Remove .html
RewriteCond     %{REQUEST_FILENAME}.html -f
RewriteRule     ^(.+?)/?$ $1.html [L]

2 个答案:

答案 0 :(得分:0)

尝试将以下内容添加到<html> <head> <title>Add Student</title> </head> <body> <form action="http://localhost/studentadded.php" method="post"> <b>Add a New Student</b> <p>First Name: <input type="text" name="first_name" size="30" value="" /></p> <p>Last Name: <input type="text" name="lastName" size="30" value="" /></p> <p>Email: <input type="text" name="email" size="60" value="" /></p> <p>Street: <input type="text" name="street" size="50" value="" /></p> <p>Province: <input type="text" name="province" size="3" value="" /></p> <p>Postal Code: <input type="text" name="postal_code" size="6" value="" /></p> <p>Phone Number: <input type="text" name="phone_num" size="20" value="" /></p> <p>Birth Date (YYYY-MM-DD): <input type="text" name="birth_date" size="20" value="" /></p> <p>Sex: <input type="text" name="sexField1" size="5" maxlength="1" value="" /> <!---<p>Sex: <input type="radio" name="sexField" value="M" /> <br> <input type="radio" name="sexField" value="F" checked /></p>---> <!--<p>Date Entered: <input type="" name="" size="" value="" /></p>---> <p>Lunch Cost: <input type="text" name="lunch_cost" size="5" value="" /></p> <p>Student ID: <input type="text" name="student_id" size="10" value="" /></p> <input type="submit" name="submitButton" value="submitValue"> <!-- type="submit" is a predefined term in html---> </form> </body> </html> 而不是上面的代码中。

.htacces

这将导致 http://www.example.com/test.html 由浏览器处理为 https://example.com/test ,同时仍会加载RewriteEngine on #Force HTTPS. RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ https://%1/$1 [R=301,L] RewriteCond %{HTTPS} !on RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] # Start rewrite. RewriteBase / # Load page without extension. RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.html -f RewriteRule ^(.+)$ $1.html [L,QSA] # 301 While Rewrite. RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/ RewriteRule ^(.*)\.html$ /$1 [R=301,L] # Handle Trailing Slashes. RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.+)/$ $1 [R,L] # Rewrite rule to add .html extension back internally. RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME}.html -f RewriteRule ^(.+[^/])$ $1.html 文件。< / p>

答案 1 :(得分:0)

将以下代码放在主目录中的.htaccess

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://yoursite.com/$1 [L,R]

#the code above will redirect the entire site into https without www unless the request come with https://www .      

RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^ https://yoursite.com/$1 [L,R]

#The code above will catch any request with www if passed first code.

RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.html[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]

#the code above will remove any .HTML.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

# you can use the two lines if file without extension not work and you want to map them to .html files and if you want to go the same file .html without extension keep it as it is but , If you want to change it to same name .php file , replace $.html in the last line to $.php .