【问题标题】:How can I redirect/ rewrite all urls that contain a certain string?如何重定向/重写包含某个字符串的所有 url?
【发布时间】:2012-03-04 19:39:27
【问题描述】:

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

我认为 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 文件仅在 URL 包含字符串“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

【问题讨论】:

    标签: php .htaccess url redirect joomla-sef-urls


    【解决方案1】:

    首先你应该删除双重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 编写的内容,因为简单的空格或缺少大写字母可能会导致无法加载任何页面的致命错误。

    希望对你有帮助。

    【讨论】:

    • 非常感谢您的帮助。我试过这个,但它给了我一个 404 错误,除了默认主页之外的任何页面。我现在意识到我问错了问题。
    • 我的个人资料位于 community/profile/user/"username" 下,除非找到包含“community/profile/user”的 URL,否则我需要 htaccess 文件什么都不做。如果找到该字符串,则它应该更改指向 mysite.com/"username" 的链接,但实际上会显示页面 index.php?option=com_comprofiler&task=userProfile&user=$2 感谢您的帮助!
    • 啊,我明白了。您正在尝试重定向和重写。无法使用您想要的方法,因为 mysite.com/username 将阻止任何其他子页面,例如 mysite.com/contact 将重定向到用户“联系人”。更简单的方法是 RewriteRule 到 "/usr/$2" [R] 然后 RewriteRule "/usr/(.*)" 到 index.php?...
    猜你喜欢
    • 2011-03-14
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-20
    • 2014-02-11
    • 2021-09-07
    • 1970-01-01
    相关资源
    最近更新 更多