从URL

时间:2018-01-31 18:46:10

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

我一直在寻找许多主题的解决方案(例如12345678)。不幸的是,我没有找到一个好的解决方案。

总体而言,我希望达到以下效果:网址开头没有www.,网址末尾没有.html/,网址包含完整路径文件(但没有显示其扩展名)。

示例: 用户输入的网址>>重定向(应加载此文件的内容)>>重定向后可在地址栏中看到的地址

  • www.example.comexample.comexample.com/example.com/index.htmlexample.com/index>> index.html>> http://example.com
  • www.example.com/abcwww.example.com/abc.html>> abc.html>> http://example.com/abc
  • www.example.com/abc/abc1www.example.com/abc/abc1.html>> abc1.html>> http://example.com/abc/abc1

我服务器上的文件结构:

 Main directory:
     index.html
     abc.html
     xyz.html
     .htaccess
     layout (there are .css, .jpg, .png ... files)
     abc:
         abc1.html
         abc2.html

我的.htaccess文件(版本1):

# rewrite engine initiation
RewriteEngine on

RewriteBase /

#canceling www
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

# handle trailing slashes (if not a directory)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R,L]

#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_FILENAME} !^.+\.html$
RewriteRule ^(.+[^/])$ $1.html

#301 from example.com/page.html to example.com/page
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)\.html$ /$1 [R=301,L]

#canceling index or index.html
RewriteRule ^index.(php|html|htm)$ / [R=301,L]
RewriteRule ^([a-z0-9-_]+)/index.(php|html|htm)$ /$1/ [R=301,L]

除了这些重定向外,几乎重定向都很有效:

我的.htaccess文件(版本2):

# rewrite engine initiation
RewriteEngine on

RewriteBase /

#canceling www
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

# handle trailing slashes (if not a directory)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R,L]

#canceling index or index.html
RewriteRule ^index.(php|html|htm)$ / [R=301,L]
RewriteRule ^([a-z0-9-_]+)/index.(php|html|htm)$ /$1/ [R=301,L]
RewriteRule ^index$ / [R=301,L]
RewriteRule ^([a-z0-9-_]+)/index$ /$1/ [R=301,L]

#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]

现在几乎重定向工作正常,但这些重定向除外:

  • www.example.com/abcwww.example.com/abc.html>> abc.html>> http://example.com/abc(我得到403错误 - abc是目录,但我也有文件abc.html)

...当我遇到内部服务器错误(500)时,有几种情况。当我在URL末尾添加/和任何错误字符时会发生这种情况。

  • www.example.com/index/anycharacter
  • www.example.com/abc/abc1/anycharacter
  • www.example.com/xyz/anycharacter

当网址末尾.html /之前www.example.com/index.html/anycharacter以及任何错误字符时,我收到404错误。这是对的。例子:

  • www.example.com/abc.html/anycharacter
  • www.example.com/abc/abc1.html/anycharacter
  • www.example.com/xyz.html/anycharacter
  • www.example.com/abc/anycharacter

此URL也出现404错误(这也是正确的,因为它与abc目录有关):

  • dat <- mutate(dat, Name <- paste(species, pop, sep = "")) dat %>% ggplot(aes(x = psd, y = val)) + geom_boxplot(outlier.colour = NA) + facet_grid(. ~ Name)

如何更改代码以使一切正常?我已经尝试这么做很长时间了。我依靠你的帮助!

0 个答案:

没有答案