![](/img/trans.png)
[英]How to redirect all urls with a certain pattern to another using mod_rewrite?
[英]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
首先,您應該刪除雙重RewriteBase
和RewriteEngine
。
現在,您的意思是這樣的:
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.