简体   繁体   中英

SEO friendly URLs being appended?

I've built the following htaccess file from googling around and looking at different examples.

My file now consists of the following rules:

## Enable Rewrite Engine
RewriteEngine on 
Options +FollowSymlinks
RewriteBase /

## Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]

## Rewrite URL to exclude wwww.
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]

## Actual URL re-writes
RewriteRule ^profile/(.*)/? profile.php?pid=$1 [QSA,L] 
RewriteRule ^profile/(.*)/(.*)/? profile.php?pid=$1&view=$2 [QSA,L]

From what I understand, this will rewrite URL's such as http://www.domain.com to http://domain.com , disable listing of directory files (+FollowSymLinks), as well as append a forward slash to urls such as http://domain.com/test and change it to http://domain.com/test/ so there's only one representation of that URL in search engines. Awesome! But, my URL keeps appending the part of the url to be re-written when I click on a link in one of my HTML breadcrumbs...

Example: http:/domain.com/profile/1/shouts/pictures/all/all/all/all/all/ if I continuously click on the same link... Why is this happening? This is issue is driving me absolutely insane!

I've tried reading mod rewrite cheatsheets and tutorials, but none of them seem to make any sense to me :(

Edit:

I should note that I'm echoing my a href code with (relative?) links. Is this the correct way of doing things?

<a href="<?php echo "profile/". $p_id."/all" ?>">All</a>

Edit 2:

I just tried using absolute links too, but getting the same issue. My PHP now reads:

<a href="<?php echo $websiteUrl. "profile/". $p_id."/all" ?>"> 

Where $websiteUrl = http://domain.com/ Any ideas anyone?

You may try this in one .htaccess file at root directory:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /

## Rewrite URL to exclude wwww.
RewriteCond %{HTTP_HOST} ^www\.domain\.com   [NC]
RewriteRule ^(.*)  http://domain.com/$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} -f  [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* -  [L]

## Add trailing slash to url
RewriteCond %{REQUEST_URI} ^/(.*[^/]+)$
RewriteRule .*             /%1/   [L]

## Actual URL re-writes
RewriteCond %{REQUEST_URI} !profile\.php [NC]
RewriteRule ^profile/([^/]+)/?$ profile.php?pid=$1  [NC,L]

RewriteCond %{REQUEST_URI} !profile\.php [NC]
RewriteRule ^profile/([^/]+)/(.*)/? profile.php?pid=$1&view=$2 [NC,L]

Your code is almost right but some minor corrections are still needed. Pls try this code:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /

## Rewrite URL to exclude wwww.
RewriteCond %{HTTP_HOST} ^www\.(domain\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=302]

RewriteCond %{REQUEST_FILENAME} -f  [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

## Add trailing slash to url
RewriteRule (?!^.*/$)^ %{REQUEST_URI}/ [L,R=302]

## Your URL re-writes
RewriteRule ^profile/([^/]+)/?$ /profile.php?pid=$1 [NC,L,QSA]

RewriteRule ^profile/([^/]+)/([^/]+)/?$ /profile.php?pid=$1&view=$2 [NC,L,QSA]

Once you verify it is working fine, replace R=302 to R=301 . Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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