Welcome to WebmasterWorld Guest from 54.158.65.139

Forum Moderators: Ocean10000 & incrediBILL & phranque

Need a hand with htaccess code please

redirect /portfolio/doamin to domain

   
2:15 pm on Feb 25, 2014 (gmt 0)



Hi,

Sorry to have to ask, but I can't seem to work this out and couldn't find how to do this on google etc.

Basically, I'm setting up a portfolio and need to redirect /portfolio/www.domain.com to www.domain.com

Now I know I can put "Redirect 301 /portfolio/www.domain.com www.domain.com" to do a single redirect, but I would like to automate this so I can enter just one line into my htaccess to redirect any link to its given domain.

For example rather then putting
Redirect 301 /portfolio/www.domain.com www.domain.com
Redirect 301 /portfolio/www.domain.com www.domain.com
Redirect 301 /portfolio/www.domain.com www.domain.com

And so on, I would like to put just the one line like:
Redirect 301 /portfolio/www.wildcard www.wildcard


This way I wouldn't have to manually enter a new line in the htaccess file each time I add another client/domain to the portfolio.

I just can't think what I would need to put in place of the last part of the original url and the url to redirect to, in order to automate any redirect including www. from a single line....

If anyone could please help me with the end part of the url for this, it would be very helpful :)
2:43 pm on Feb 25, 2014 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Use a RewriteRule configured to deliver a 301 redirect.

With RegEx pattern matching you can capture and re-use parts of the originally requested URL in the target redirected-to URL.
2:56 pm on Feb 25, 2014 (gmt 0)



Thanks g1smd, please could you let me know the code for that or link to where I can see some examples so I can test it and get it right first time?

Please note, I have to do this in a directory level htaccess file due to some conflicts in the one in my root (if that makes a difference?)
3:10 pm on Feb 25, 2014 (gmt 0)



I had a read about the RegEx pattern matching and thought may this should work:

RewriteRule /portfolio/www.^(.*)$ http://www.^(.*)$


But it did nothing... have I missed something out, or is this not correct at all?!
3:36 pm on Feb 25, 2014 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /portfolio
RewriteRule ^portfolio/(.+) http: //$1 [R=301,L]


This should go near the beginning of the root htaccess file.

If non-www is requested, should this redirect too?

If so, replace with...

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /portfolio
RewriteRule ^portfolio/(www\.)?(.+) http: //www.$2 [R=301,L]


Requesting either example.com/portfolio/example.net/ or example.com/portfolio/www.example.net/ will redirect to www.example.net/ in a single step.

Remove space between colon and double slash.
4:20 pm on Feb 25, 2014 (gmt 0)



Thank you so much g1smd :)

I will give it a shot in a bit as just need to get to the shop, but will that work only if the end url has www. because I forgot to mention I need it to only work if the url ends with a domain as their are other site pages such as /contact.php

All the client urls will start with www. when linked to in the portfolio.
4:43 pm on Feb 25, 2014 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Use the second ruleset but remove the question mark from the RegEx pattern.

Remove the brackets from around www\. and change $2 to $1.
5:03 pm on Feb 25, 2014 (gmt 0)



Thanks, but I get an internal error across the whole domain when I enter that in my htaccess http://www.example.co.uk/portfolio/

This is my full htaccess, do I need to move the last bit to the top?
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
#
# Rewrite .php to extensionless URL:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
#
# Externally redirect requests for .php URLs to extensionless URLs:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php http://www.example.co.uk/$1 [R=301,L]
# Above line has to use your url not HTTP_HOST as that dont work!

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /portfolio
RewriteRule ^portfolio/www\.(.+) http: //www.$1 [R=301,L]

[edited by: phranque at 12:45 am (utc) on Feb 26, 2014]
[edit reason] Please Use example.com [webmasterworld.com] [/edit]

5:13 pm on Feb 25, 2014 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



From my second post...
This should go near the beginning of the root htaccess file.

Your rulesets 1 - 2 - 3 - 4 after
RewiteEngine On
should be ordered 4 - 3 - 1 - 2.

and Remove space between colon and double slash. (but NOT here in the forum).

[edited by: g1smd at 5:22 pm (utc) on Feb 25, 2014]

5:17 pm on Feb 25, 2014 (gmt 0)



Thanks, but I did try that as below, or have I mixed it all up now...
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# This is for redirecting client urls in the portfolio to their websites
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /portfolio
RewriteRule ^portfolio/www\.(.+) http: //www.$1 [R=301,L]
#
# Externally redirect requests for .php URLs to extensionless URLs:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php http://www.example.co.uk/$1 [R=301,L]
# Above line has to use your url not HTTP_HOST as that dont work!
#
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
#
# Rewrite .php to extensionless URL:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
#


I didn't even see that space, its removed now on my site, which has fixed the internal error, but the redirect isn't working. Have I still got bits in the wrong order?

Thanks again for all the time you are spending to help me :)

[edited by: phranque at 12:46 am (utc) on Feb 26, 2014]
[edit reason] Please Use example.com [webmasterworld.com] [/edit]

5:46 pm on Feb 25, 2014 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



As long as this code is in the root htaccess and there are no other rules elsewhere it should work as described.

The comment on the final ruleset is exactly backwards. The rule rewrites extensionless requests to internally fetch content from PHP files.

The target of the final rule should begin with a slash. The NC flag on the final rule is redundant.
7:19 pm on Feb 25, 2014 (gmt 0)



Thanks got it all working now :)

Yes I need to change that comment, I know it removes it, I just put the wrong comment in for some silly reason - I think I was half asleep when doing it originally, which is why I got this in such a mess before your help lol

Thank you again so much :)
7:44 pm on Feb 25, 2014 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Perhaps you could post the final fixes?
8:34 pm on Feb 25, 2014 (gmt 0)



Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# This is for redirecting client urls in the portfolio to their websites:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /portfolio
RewriteRule ^portfolio/www\.(.+) http://www.$1 [R=301,L]
#
# Externally redirect requests for .php URLs to extensionless URLs:
#RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
#RewriteRule ^(([^/]+/)*[^.]+)\.php http://www.example.co.uk/$1 [R=301,L]
# Above line has to use your url not HTTP_HOST as that dont work!
#
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
#
# Rewrite rewrites extensionless requests to internally fetch content from PHP files:
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^([^\.]+)$ $1.php [L]
#


Note I had to comment out the last bit as it wouldn't work, but I think that's to do with the script I'm using for the portfolio - I will dig some more tomorrow maybe to see why, but had enough for tonight :)

[edited by: phranque at 12:46 am (utc) on Feb 26, 2014]
[edit reason] Please Use example.com [webmasterworld.com] [/edit]

10:41 pm on Feb 25, 2014 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^([^\.]+)$ $1.php [L]

Do you actually have physical files whose names don't include an extension? If not, you can omit that server-hogging -f condition, because you already know the file doesn't exist. Constrain the rule to specific directories if it's only intended to apply in some areas.

RewriteRule ^portfolio/www\.(.+) http://www.$1 [R=301,L]

I'm overlooking something obvious here, but why doesn't this rule result in sending people to some entirely unrelated site? Are all those portfolio domains yours too? How many of them are there?

[edited by: phranque at 12:47 am (utc) on Feb 26, 2014]
[edit reason] unlinked url for clarity [/edit]

12:35 pm on Feb 26, 2014 (gmt 0)



The files within the portfolio script exist with the extension, index.php, about.php and contact.php so I'm not sure why its not working with the script (I didn't make the script, its something I bought some years back so would have to look into how its done I guess to see what's conflicting with it?)

Not sure what you mean about the external links? If you go to http://www.example.co.uk/portfolio/ and click the client link, it redirects to their site, which is what I need it to do. No they aren't my domains too, they are/will be client sites once the full site is up and running.

[edited by: phranque at 5:06 pm (utc) on Feb 26, 2014]
[edit reason] WWW.EXAMPLE.CO.UK please [/edit]

12:49 pm on Feb 26, 2014 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



The target of the final rule should begin with a slash.
1:03 pm on Feb 26, 2014 (gmt 0)



Would that be
RewriteRule ^([^\.]+)$ /$1.php [L]
then?
5:04 pm on Feb 26, 2014 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



IMPORTANT: Please Use Example.com For Domain Names in Posts:
http://www.webmasterworld.com/apache/4452736.htm [webmasterworld.com]
5:45 pm on Feb 26, 2014 (gmt 0)



Thanks phranque, but I was posting my domain to get help with it and show what was happening, I thought this should be ok as I was having problems and its quicker to fix if people can see what was happening...
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month