如何删除php扩展并将扩展名重定向到无扩展名表单

时间:2015-10-08 16:26:11

标签: apache .htaccess mod-rewrite url-redirection

我怎么能在我的htaccess中拥有这个:

/page.php => redirects to /page
/page => shows the page.php
/index => redirects to root

如何从所有网址中删除尾部斜杠,特别是根网址?

虽然,我对Apache配置不够熟悉,但我可以找到这些代码:

# To remove .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

# To remove trailing slashes
RewriteRule ^(.*)/$ /$1 [L,R=301]

# To redirect /index to root (not sure if it works correctly)
RewriteCond %{THE_REQUEST} ^.*/index
RewriteRule ^(.*)index.php$ /$1 [R=301,L]

你可以帮我解决一下这个场景,并将其放在一个小代码中吗?

由于

根据答案,我可以使用以下代码在我的.htaccess文件中解决问题:

# To remove .php extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ (.*)\.php [NC]
RewriteRule ^ %1 [R=301,L]

# To remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]

# To check whether the file exists then set it back internally
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^.*$ $0.php [L]

# To redirect /index to root
RewriteCond %{THE_REQUEST} ^.*/index
RewriteRule ^(.*)index.php$ /$1 [R=301,L]

现在,我遇到了一个新问题,就是当我使用尾部斜杠浏览文件时,它会将我重定向到其他地方并发生404(找不到页面)响应,如下所示:

http://domain.tld/page/ => http://domain.tld/page/home/user/public_html/domain.tld/page

此外,如果我将第二部分移动到最后甚至完全删除它,服务器会向我发送500(内部服务器错误)响应。

最后通过一些研究,我可以使用这个建议找到以前问题的解决方案:

https://stackoverflow.com/a/27264788/5420319

所以,我已经改变了其中一个代码:

# To remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]

对此:

# To remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)/$
RewriteRule ^ /%1 [R=301,L]

而且无论是否有效,我都将这部分移到了最前面。

4 个答案:

答案 0 :(得分:1)

page.php重定向到/page

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /page\.php [NC]
RewriteRule ^ /page [R=301,L]

现在,在内部设置为page.php

RewriteRule ^page$ /page.php [NC,L]

删除尾部斜杠:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]

现在,检查/some/page/foo的文件是否存在/some/page/foo.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^.*$ $0.php [L]

答案 1 :(得分:1)

已接受答案的清理和简化版本是:

@NotNull
@Size(min = 2, max = 256, message = "{validation.length}")
private String name;

@Size(min = 6, max = 32, message = "{validation.length}")
private String companyId;

@NotNull
private String country;

@NotNull
private String city;

@NotNull
private String state;

@NotNull
private String postalCode;

@NotNull
private String address1;

private String address2;

@NotNull
private String phone1;

private String phone2;

private String fax;

@Email(message = "{validation.invalidChars}")
private String email;

private String website;

private MultipartFile companyLogo;

private boolean status;

答案 2 :(得分:0)

根据答案和我自己的研究,这将是最终答案:

# To remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)/$
RewriteRule ^ /%1 [R=301,L]

# To remove .php extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ (.*)\.php [NC]
RewriteRule ^ %1 [R=301,L]

# To check whether the file exists then set it back internally
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^.*$ $0.php [L]

# To redirect /index to root
RewriteCond %{THE_REQUEST} ^.*/index
RewriteRule ^(.*)index.php$ /$1 [R=301,L]

答案 3 :(得分:-1)

Options +FollowSymLinks
RewriteEngine on
AddHandler php5.3-fastcgi php
SetEnv no-gzip dont-vary

RewriteRule ^([a-z]+)$                                                  index.php?page=$1                                   [QSA,L]
RewriteRule ^([a-z]+)/([a-z0-9]+)$                                      index.php?page=$1&subpage=$2                        [QSA,L]
RewriteRule ^([a-z]+)/([a-z0-9]+)/([a-z0-9]+)$                          index.php?page=$1&subpage=$2&module=$3              [QSA,L]
RewriteRule ^([a-z]+)/([a-z0-9]+)/([a-z0-9]+)/([a-z0-9]+)$              index.php?page=$1&subpage=$2&module=$3&submodule=$4 [QSA,L]

你可以玩那个。