简体   繁体   English

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

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

this has had me stumped for a few days. 这让我难过了几天。 Any help would be greatly appreciated. 任何帮助将不胜感激。

I think htaccess is probably the best way to do what I need, but if you have a different sdolution I'm glad to hear it. 我认为htaccess可能是我需要做的最好方法,但是如果您有不同的建议,我会很高兴听到它。

I am using joomla as my CMS and right now I have a .htacces file right now that will take all urls and send them to the community component of my site. 我正在使用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

However this will redirect every page Url that doesn't have a directory or file associated with it. 但是,这将重定向每个没有目录或文件关联的页面URL。

What I need is for the htaccess file to only redirect URLS if they contain the string "community/profile/user" 我需要的是htaccess文件仅在包含字符串“ community / profile / user”的情况下才重定向URL。

I still consider myself a noob, and I have spent many days already trying to fix this problem. 我仍然认为自己是菜鸟,而且我已经花了很多天试图解决这个问题。

Hopefully someone else can shed some light on the issue here. 希望其他人可以在这里阐明这个问题。 Below is the complete code of my .htaccess file 以下是我的.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

First of all you should remove the double RewriteBase and RewriteEngine . 首先,您应该删除双重RewriteBaseRewriteEngine

Now, did you mean something like this: 现在,您的意思是这样的:

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]

The first Rewrite shouldn't change a thing about the path of all the image files. 第一次重写不应该改变所有图像文件的路径。 If the URL contains community/profile/user/ then it rewrite the url to index.php?option=com_comprofiler&task=userProfile&user={what-comes-after-user/} 如果该URL包含community/profile/user/则它将URL重写为index.php?option=com_comprofiler&task=userProfile&user={what-comes-after-user/}

You should be very carefull on what you write with .htaccess because a simple space or a missing capital can end up in a fatal error not loading any pages. 您应该对使用.htaccess编写的内容非常谨慎,因为简单的空格或大写字母可能会导致致命错误,导致无法加载任何页面。

I hope this helps you. 我希望这可以帮助你。

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

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