homepage Welcome to WebmasterWorld Guest from 23.22.128.96
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Need a hand with htaccess code please
redirect /portfolio/doamin to domain
NathanielB




msg:4648904
 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 :)

 

g1smd




msg:4648924
 2:43 pm on Feb 25, 2014 (gmt 0)

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.

NathanielB




msg:4648927
 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?)

NathanielB




msg:4648930
 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?!

g1smd




msg:4648933
 3:36 pm on Feb 25, 2014 (gmt 0)

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.

NathanielB




msg:4648949
 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.

g1smd




msg:4648960
 4:43 pm on Feb 25, 2014 (gmt 0)

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

Remove the brackets from around www\. and change $2 to $1.

NathanielB




msg:4648975
 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]

g1smd




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

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]

NathanielB




msg:4648979
 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]

g1smd




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

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.

NathanielB




msg:4649029
 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 :)

g1smd




msg:4649042
 7:44 pm on Feb 25, 2014 (gmt 0)

Perhaps you could post the final fixes?

NathanielB




msg:4649067
 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]

lucy24




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

#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]

NathanielB




msg:4649268
 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]

g1smd




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

The target of the final rule should begin with a slash.

NathanielB




msg:4649304
 1:03 pm on Feb 26, 2014 (gmt 0)

Would that be
RewriteRule ^([^\.]+)$ /$1.php [L] then?
phranque




msg:4649409
 5:04 pm on Feb 26, 2014 (gmt 0)

IMPORTANT: Please Use Example.com For Domain Names in Posts:
http://www.webmasterworld.com/apache/4452736.htm [webmasterworld.com]

NathanielB




msg:4649424
 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...

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved