繁体   English   中英

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

[英]How can I redirect/ rewrite all urls that contain a certain string?

这让我难过了几天。 任何帮助将不胜感激。

我认为htaccess可能是我需要做的最好方法,但是如果您有不同的建议,我会很高兴听到它。

我正在使用joomla作为CMS,现在我有了一个.htacces文件,它将接收所有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文件仅在包含字符串“ community / profile / user”的情况下才重定向URL。

我仍然认为自己是菜鸟,而且我已经花了很多天试图解决这个问题。

希望其他人可以在这里阐明这个问题。 以下是我的.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编写的内容非常谨慎,因为简单的空格或大写字母可能会导致致命错误,导致无法加载任何页面。

我希望这可以帮助你。

暂无
暂无

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

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