使用.htaccess中的重写规则自定义网址

时间:2017-08-16 08:47:52

标签: php apache .htaccess mod-rewrite url-rewriting

基本问题我正在努力寻找解决方案。

我有一个网址www.example.com/signup/form.html我想重写网址为www.example.com/signup从网址中删除form.html但直接转到此html文件。

我已经尝试过一些琐事,我的根目录中有一个.htaccess文件RewriteEngine on

由于某种原因,它加载为www.example.com/signup并突然重定向到404 - 找不到页面。

这是我目前使用的(不工作):

RewriteRule ^signup/form.html.*$ http://www.example.com/signup/ [R=301,L]

还试过这个:

RewriteRule ^signup/(.*)$ http://www.example.com/signup [R=301,L]

此外,我在.htaccess文件的顶部有这个,这可能是我获得404的原因吗?

<Files ~ "^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
satisfy all
</Files>
<Files ~ "^.ht">
Order allow,deny
Deny from all
</Files>
<Files .htaccess>
order allow,deny
deny from all
</Files> 
<Files ~ "includes$">
Order allow,deny
Deny from all
</Files>
<Files ~ "mods$">
Order Deny,Allow
Deny from all
</Files>
php_flag session.use_trans_sid off
php_flag error_reporting off
Options -Indexes
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
FileETag none
ExpiresActive On
ExpiresDefault A0
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/ico A2592000
ExpiresByType text/css A2592000
ExpiresByType text/javascript A2592000

<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Expires "Thu, 15 Jan 2035 20:00:00 GMT"
</FilesMatch>

ErrorDocument 404 /notfound

DirectoryIndex index.php
RewriteEngine on 
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]



Redirect 301 /default.aspx http://www.example.com
Redirect 301 /home.asp http://www.example.com

Redirect 301 /contact.aspx http://www.example.com/contact/enquire
Redirect 301 /awards.aspx http://www.example.com/awards
Redirect 301 /event.asp http://www.example.com/visitus/events

Redirect 301 /social.aspx http://www.example.com/visitus/events
Redirect 301 /recipes.aspx‎ http://www.example.com/news/09/recipes
Redirect 301 recipes.aspx‎ http://www.example.com/news/09/recipes
Redirect 301 /recipes.asp http://www.example.com/news/09/recipes
Redirect 301 recipes.asp http://www.example.com/news/09/recipes
Redirect 301 /styles http://www.example.com/


Redirect 301 /home.asp?pid=24&page=4 http://www.example.com
Redirect 301 /home.asp?pid=13&page=2 http://www.example.com
Redirect 301 /home.asp?pid=42&page=3 http://www.example.com
Redirect 301 /home.asp?pid=12&page=1 http://www.example.com
Redirect 301 /home.asp?pid=71&page=3 http://www.example.com
Redirect 301 /event.asp?pid=30&page=2 http://www.example.com/visitus/events
Redirect 301 /event.asp?pid=63 http://www.example.com/visitus/events


RewriteRule ^pg([0-9]+)$ index.php?content=page&id=$1
RewriteRule ^([a-z]+)-pg([0-9]+)$ index.php?content=$1&pg=$2
RewriteRule ^([a-z]+)-([0-9]+)$ index.php?content=$1&id=$2




RewriteRule ^([a-z]+)/0([1-9]|[1-9]([0-9]+))/pg([0-9]+)/([a-z0-9\-\/]+)$ index.php?content=$1&cat=$2&pg=$4

RewriteRule ^([a-z]+)/0([1-9]|[1-9]([0-9]+))/([a-z0-9\-\/]+)$ index.php?content=$1&cat=$2
RewriteRule ^sitemap.xml sitemap.php

RewriteRule ^([a-z]+)-day-([0-9]{4})-([0-9]{2})-([0-9]{2})$ index.php?content=$1&view=day&y=$2&m=$3&d=$4
RewriteRule ^([a-z]+)-month-([0-9]{4})-([0-9]{2})$ index.php?content=$1&view=month&y=$2&m=$3
RewriteRule ^([a-z]+)-year-([0-9]{4})$ index.php?content=$1&view=year&y=$2

RewriteRule ^([a-z]+)-archs([0-9]+)$ index.php?content=$1&view=archives&pg=$2

RewriteRule ^images/lightbox-btn-next.gif images/lightbox-btn-next.png
RewriteRule ^images/lightbox-btn-prev.gif images/lightbox-btn-prev.png
RewriteRule ^images/lightbox-btn-close.gif images/lightbox-btn-close.png

DirectorySlash On
DirectoryIndex form.html

我对此进行了研究here,但我不知道我正在努力使语法正确。

3 个答案:

答案 0 :(得分:1)

你可以用这个来实现:

RewriteEngine On
RewriteRule ^index\.html$ / [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]

重写规则检查以/index.html结尾的任何请求,并删除index.html位(如果找到)。

确保在测试之前清除缓存。

编辑:

请尝试这一点,这应涵盖.html.php

RewriteCond %{THE_REQUEST} \/index\.(php|html)\ HTTP [NC]
RewriteRule (.*)index\.(php|html)$ /$1 [R=301,L]

答案 1 :(得分:1)

我以一种不太想要的方式解决了这个问题,但无论如何,我的解决方案是将我的.html文件添加到根目录(不在任何目录中)并将文件重命名为注册.html和使用过的.htaccess重写以删除.html

  

.htaccess(重写)

 DirectoryIndex index.php
 RewriteEngine on
 RewriteCond %{REQUEST_FILENAME}.html -f
 RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]

还为已拥有以前地址的任何人添加了重定向:

  

.htaccess(重定向)

Redirect 301 /signup/index.html http://www.exapmle.com/signup

我真的不想从子目录中移动我的.html文件,但这是我唯一的“工作”解决方案。

答案 2 :(得分:0)

获取它的正确.htaccess语法是:

RewriteRule ^/signup(.*)$ /signup/index.html [R=301,L]

这意味着/ signup和/ signup /都将重定向到/signup/index.html