如何重定向/重写所有包含特定字符串的url



这让我困惑了几天。如有任何帮助,我们将不胜感激。

我认为htaccess可能是做我需要的事情的最好方法,但如果你有不同的解决方案,我很高兴听到。

我使用joomla作为我的CMS,现在我有一个.htaces文件,它将接收所有URL并将它们发送到我网站的社区组件。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?option=com_comprofiler&task=userProfile&user='$1' [L]

# RewriteRule ^(.*) index.php?cb_username=$1
RewriteCond %{REQUEST_FILENAME} !.(jpg|jpeg|gif|png|css|js|pl|txt)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php

然而,这将重定向每个没有相关目录或文件的页面Url。

我需要的是htaccess文件只重定向URLS,如果它们包含字符串"community/profile/user"

我仍然认为自己是个傻瓜,我已经花了很多天的时间试图解决这个问题。

希望其他人能在这里阐明这个问题。以下是我的.htaccess文件的完整代码

 ##
# @version $Id: htaccess.txt 1005 2006-10-05 18:00:00Z stingrey $
# @package Joomla
# @copyright Copyright (C) 2006 Open Source Matters. All rights reserved.
# @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
# Joomla! is Free Software
##
Options +FollowSymLinks
#
# mod_rewrite in use
#
RewriteEngine On
# Uncomment following line if your webserver's URL
# is not directly related to physical file paths.
# Update YourJoomlaDirectory (just / for root)
RewriteBase /
#
# Rules
#
#
# ProfileRedirector V 1.3
#
# Make sure your host allows option override
# and has ModRewrite installed
# activate Rewrite enginge
RewriteEngine On

RewriteBase /
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?option=com_comprofiler&task=userProfile&user='$1' [L]

# RewriteRule ^(.*) index.php?cb_username=$1
RewriteCond %{REQUEST_FILENAME} !.(jpg|jpeg|gif|png|css|js|pl|txt)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php

首先,您应该删除双RewriteBaseRewriteEngine

你的意思是这样的吗:

Options +FollowSymLinks
RewriteBase /
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} .(jpg|jpeg|gif|png|css|js|pl|txt)$
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ $1 [L]
RewriteRule ^(.*)community/profile/user/(.*)$ index.php?option=com_comprofiler&task=userProfile&user=$2 [L]

第一次"重写"不应该改变所有图像文件的路径。如果URL包含community/profile/user/,则将URL重写为index.php?option=com_comprofiler&task=userProfile&user={what-comes-after-user/}

你应该非常小心地使用.htaccess写什么,因为一个简单的空格或缺少大写字母可能会导致无法加载任何页面的致命错误。

我希望这对你有帮助。

最新更新