2012-03-04 264 views
0

這讓我難倒了幾天。任何幫助將不勝感激。如何重定向/重寫包含特定字符串的所有網址?

我認爲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和我花了很多天已經試圖解決這一問題問題。

希望別人能夠在這裏解決這個問題。 下面是我的.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 

回答

0

首先的完整代碼,你應該刪除雙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寫的東西很小心,因爲一個簡單的空間或缺少資本可以致命錯誤不加載任何頁面結束。

我希望這可以幫助你。

+0

非常感謝您的幫助。我嘗試了這個,但它給了我一個404錯誤以前的頁面,其他的默認主頁。我現在意識到我提出了錯誤的問題。 – user695695 2012-03-05 00:13:11

+0

我的個人資料位於community/profile/user /「username」下,除非找到包含「community/profile/user」的URL,否則我需要htaccess文件不做任何事情。如果找到該字符串,它應該鏈接到mysite.com/「用戶名」的鏈接,但真正顯示頁面index.php?option = com_comprofiler&task = userProfile&user = $ 2感謝您的幫助! – user695695 2012-03-05 00:18:47

+0

啊我明白了。你正在嘗試重定向和重寫。無法使用所需的方法,因爲mysite.com/username會阻止任何其他子頁面,例如mysite.com/contact將重定向到用戶「聯繫人」。更簡單的方法是將RewriteRule設置爲「/ usr/$ 2」[R],然後將RewriteRule「/usr/(.*)」設置爲index.php?... – 2012-03-05 08:59:15

相關問題