简体   繁体   English

如何使用htaccess将root和/index.php显示为/ news?

[英]How to use htaccess to make root and /index.php display as /news?

My news page can be reached on these urls: www.domain.com/index.php and www.domain.com/ (both exactly the same page). 可以在以下URL上访问我的新闻页面: www.domain.com/index.phpwww.domain.com/ (两者完全相同)。

I want to make these urls a little prettier, so I want them both to redirect to www.domain.com/news . 我想使这些网址更漂亮,所以我希望它们都重定向到www.domain.com/news

So: 所以:

  1. I want to use htaccess to make sure that people who type: www.domain.com/news in the address bar, see the contents of www.domain.com/index.php . 我想使用htaccess来确保在地址栏中键入www.domain.com/news人,请参阅www.domain.com/index.php的内容。

  2. I want to use htaccess to redirect everyone from the ugly urls (when they try to access them directly) to the pretty one, so people who type: www.domain.com/index.php or www.domain.com/ get redirected to www.domain.com/news 我想使用htaccess将所有人从丑陋的URL(当他们尝试直接访问它们时)重定向到漂亮的URL,以便键入以下内容的人: www.domain.com/index.phpwww.domain.com/重定向到www.domain.com/news

Of course this has a potential for looping, so how do I do this properly? 当然,这可能会造成循环,那么我该如何正确执行呢?


background information: these are the rules I've already got in place (and I'm using modx as a cms): 背景信息:这些是我已经制定的规则(并且我使用modx作为cms):

#########################
# Turn on URL rewriting #
#########################

RewriteEngine On
RewriteBase /


#####################################
# Convert caps to lowercase letters #
#####################################

# If there are caps, set HASCAPS to true and skip next rule
RewriteRule [A-Z] - [E=HASCAPS:TRUE,S=1]

# Skip this entire section if no uppercase letters in requested URL
RewriteRule ![A-Z] - [S=28]

# Replace single occurance of CAP with cap, then process next Rule.
RewriteRule ^([^A]*)A(.*)$ $1a$2
RewriteRule ^([^B]*)B(.*)$ $1b$2
RewriteRule ^([^C]*)C(.*)$ $1c$2
RewriteRule ^([^D]*)D(.*)$ $1d$2
RewriteRule ^([^E]*)E(.*)$ $1e$2
RewriteRule ^([^F]*)F(.*)$ $1f$2
RewriteRule ^([^G]*)G(.*)$ $1g$2
RewriteRule ^([^H]*)H(.*)$ $1h$2
RewriteRule ^([^I]*)I(.*)$ $1i$2
RewriteRule ^([^J]*)J(.*)$ $1j$2
RewriteRule ^([^K]*)K(.*)$ $1k$2
RewriteRule ^([^L]*)L(.*)$ $1l$2
RewriteRule ^([^M]*)M(.*)$ $1m$2
RewriteRule ^([^N]*)N(.*)$ $1n$2
RewriteRule ^([^O]*)O(.*)$ $1o$2
RewriteRule ^([^P]*)P(.*)$ $1p$2
RewriteRule ^([^Q]*)Q(.*)$ $1q$2
RewriteRule ^([^R]*)R(.*)$ $1r$2
RewriteRule ^([^S]*)S(.*)$ $1s$2
RewriteRule ^([^T]*)T(.*)$ $1t$2
RewriteRule ^([^U]*)U(.*)$ $1u$2
RewriteRule ^([^V]*)V(.*)$ $1v$2
RewriteRule ^([^W]*)W(.*)$ $1w$2
RewriteRule ^([^X]*)X(.*)$ $1x$2
RewriteRule ^([^Y]*)Y(.*)$ $1y$2
RewriteRule ^([^Z]*)Z(.*)$ $1z$2

# If there are any uppercase letters, restart at very first RewriteRule in file.
RewriteRule [A-Z] - [N]

RewriteCond %{ENV:HASCAPS} TRUE
RewriteRule ^/?(.*) /$1 [R=301,L]


######################################
# Convert domain to prefixed version #
######################################

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


##############################################################
# Redirect .html requests to equivalent pages without suffix #
##############################################################

RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*)\.html$ $1 [R=301,L]


##########################
# The Friendly URLs part #
##########################

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA,NC]


##########################################
# Serve .htc files with proper MIME type #
##########################################

# Critical for XP SP2 (use if your host allows htaccess MIME type overrides).
AddType text/x-component .htc


#######################
# Turn compression on #
#######################

php_flag zlib.output_compression On
php_value zlib.output_compression_level 5

You can insert this simple rule: 您可以插入以下简单规则:

Redirect /index.php http://www.domain.com/news.php
DirectoryIndex news.php

You need to set a specific rule. 您需要设置特定的规则。

Something like this in your case in the .htaccess 在.htaccess中,这种情况

RewriteRule ^news/?$ index.php [R=301,NC,L]

I have not checked this. 我还没有检查。

Insert this code after RewriteBase line: 在RewriteBase行之后插入以下代码:

# external redirect from /index.php to /news
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(index\.php)?[?\s] [NC]
RewriteRule ^ /news [R=301,L]

# internal forward from  /news to /index.php
RewriteRule ^news/?$ /index.php [NC,L]

You can set your site start to be any page you wish. 您可以将站点开始设置为您希望的任何页面。 I'd just go to System Settings, make /news/ the homepage and you should be all set. 我只需要转到“系统设置”,进入/ news /主页,就可以完成所有设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM