Forum Moderators: phranque

Message Too Old, No Replies

htaccess match url & append

         

wannabem

5:25 pm on Apr 3, 2010 (gmt 0)

10+ Year Member



Hi,

I have been searching the internet to get the following done, but i am not finding any solution. there is a lot of material how to change dynamic url to static.

i have to do the following:

if it is

subdomain.mydomain.com
then change to
subdomain.mydomain.com/index.php?show=blog

and if it is
subdomain.mydomain.com/index.php
change to
subdomain.mydomain.com/index.php?show=blog

if anyone can help me out with the correct htaccess lines, you will make me extremely happy :-)

rgds,

W

g1smd

5:44 pm on Apr 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What code have you tried already? We can help you get your code working, but there's not enough volunteers to provide a free code writing service.

Having said that, this is a topic that arises several times each month, and as such there are hundreds of prior example threads explaining what you need to do.

However, your 'specification' is very muddled and not at all clear. You need to clearly defi8ne the URL formats the site will use and the server internal filepath that will service those requests. Take care with catering for how robots.txt, image, CSS and JS files are handled, as well as searchengine verification file requests.

jdMorgan

5:54 pm on Apr 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, note that the main "danger" here is your use of the words "change to."

Properly-implemented, you will change nothing. You will simply map client requests for certain URLs to the index.php filepath with a query string of "show=blog" using an internal rewrite -- and not a redirect.

The general form, using Apache mod_rewrite, is

RewriteRule ^client-requested-url-path$ /path-to-script?query-string [L]

The resources in our Apache Charter and in our Apache Forum Library may help you get started. See the links at the top of this page.


Jim

wannabem

6:30 pm on Apr 3, 2010 (gmt 0)

10+ Year Member



yes you guys are right, excuse me for not giving any code that i tried so far

I tried the following :

RewriteCond %{HTTP_HOST} ^subdomain\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ [subdomain.mydomain.com...] [L,R=301]

But my browser gives the following error message :
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

In Opera browser i see that the url is changed into subdomain.mydomain.com/index.php?show=blog but it is not navigating to that page

what is wrong?

g1smd

6:57 pm on Apr 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You first need to define whether you are thinking about redirects or rewrites.

jdMorgan supplied an example of a rewrite, and you responded with an example of a redirect.

My advice right now would be to stop coding and instead clearly define you goals in terms of
- URLs used on the web (including domain name), and files inside the server (without any domain name), and their relationship for use in rewrites, and
- URL requests from the web, and the new URL that request should be redirected to for your redirects.

wannabem

7:11 pm on Apr 3, 2010 (gmt 0)

10+ Year Member



hi g1smd,

I provided the code that i tried so far, as you asked:
"What code have you tried already? We can help you get your code working, but there's not enough volunteers to provide a free code writing service."

I do have some basic programming experience but htacces seems to be a monster that is hard to master ;-)

but alright, i will dig through some sections.

The goal is simple: i explained what i want in my first post.

subdomain.mydomain.com -> subdomain.mydomain.com/index.php?show=blog

subdomain.mydomain.com/index.php -> subdomain.mydomain.com/index.php?show=blog

jdMorgan

8:14 pm on Apr 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Put your URL-path into the code I posted on the 'left side', and put your script filepath an query into the 'right side.' Keep your RewriteCond as you posted it. Don't change anything else.

This is an internal URL-to-filepath translation, and not a redirect. Therefore, if you see your browser address bar change, you have mis-coded something.

Read the replies here in detail. They are written in detail, and if you miss something, then that will cost everyone time, and may cost you money (in the long term). If a detail is not clear, then research it, and if that doesn't answer the question, then feel free to ask.

BTW, one little typo or logic error in your sever configuration code --and that's what this is-- can put you out of business. Therefore, the details matter...

Jim

wannabem

9:53 pm on Apr 3, 2010 (gmt 0)

10+ Year Member



I have one part working

www.example.com/index.php - >www.example.com/index.php?show=blog

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.(htm(l)?|php)\ HTTP/
RewriteRule ^(([^/]+/)*)index\.(htm(l)?|php)$ http://www.example.com/index.php?show=blog$1 [R=301,L]

Now the second part is not really working as i need it to be working

I need it to be a redirect. i have tried :

RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^*.$ http://www.example.com/index.php?show=blog [R=301,L]

But this is giving me an error.

anyone any ideas?

[edited by: jdMorgan at 2:18 am (utc) on Apr 4, 2010]
[edit reason] example.com [/edit]

jdMorgan

2:17 am on Apr 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Of course it is. Your RewriteRule pattern matches "anything, everything, or nothing" and redirects that to index.php, which will match "anything, everything, or nothing" and therefore get redirected again and again.

It is usually an error to redirect to a script filepath. The vast majority of Webmasters asking about "site fix-up" code here are trying to get rid of ugly script+query string URLs.

Jim

wannabem

7:18 am on Apr 4, 2010 (gmt 0)

10+ Year Member



but the rewritecond is www.example.com so the rewriterule doesnt start working before that condition is matched, right?

And if it is matched to www.example.com then the rewrite may rewrite it completely to http://www.example.com/index.php?show=5

g1smd

7:23 am on Apr 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm still not clear, and as far as I can tell neither is the poster, as to whether they want a redirect or a rewrite.

wannabem

7:35 am on Apr 4, 2010 (gmt 0)

10+ Year Member



External redirect = Tell client to ask for the requested content again using a new URL and HTTP request.
Internal rewrite == Get content for requested URL from a different server filepath than implied by the requested URL.

A redirect will fit my purpose.

wannabem

8:34 am on Apr 4, 2010 (gmt 0)

10+ Year Member



I simply do not get it

RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$ [NC]
RewriteRule ^(.*)$
http://www.mydomain.com/index.php
[R=301,L]

The above gets into a loop.. but why? the url is rewritten into www.mydomain.com/index.php so the RewriteCond should never match the new url

I also tried:
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$ [NC]
RewriteRule ^www\.mydomain\.com$
http://www.mydomain.com/index.php
[R=301,L]

Which doesnt work as well

redirecting to another domain e.g.
RewriteRule ^(.*)$
http://www.myotherdomain.com/index.php
[R=301,L]

that one works, logically and obviously because the new domain will not encounter the htaccess script.

I have been digging through webmasterworld like a maniac since yesterday, and i just cannot find the solution.

wannabem

9:03 am on Apr 4, 2010 (gmt 0)

10+ Year Member



got it to work somehow

RewriteRule ^$ /index.php [R=301] <- change www.mydomain.com into www.mydomain.com/index.php

the code below will check for index.php/htm/html and if so it will change to index.php?show=blog

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.(htm(l)?|php)\ HTTP/
RewriteRule ^(([^/]+/)*)index\.(htm(l)?|php)$
http://www.mydomain.com/index.php?show=blog
[R=301,L]

What do you guys think, will this work well? :-)

rgds,

W

g1smd

11:09 am on Apr 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You keep on talking about 'rewrites' but every piece of code you post is for a 'redirect'.

You've been advised - no less than five times - to use a rewrite. A rewrite maps an external URL request to an internal filepath. A rewrite does not introduce a new URL.

I cannot really fathom out what you want to achieve here, and you have failed to answer any of the questions I have already asked.

I do not know why when a visitor asks for
www.example.com/
you would want the visitor to make a new request for a different and longer URL.

When the user requests
www.example.com/
I'd use a rewrite to fetch the content for the currently requested URL, the rewrite silently fetching content from a different server-internal filepath other than that suggested by the path part of the URL request.

I am fairly sure that
DirectoryIndex /index.php?show=blog
would have been a far better solution, but using an internal rewrite could also work well.

However you do this, you would still need a set of canonicalisation redirects to prevent the URL
(www.)example.com/index.php
and/or
(www.)example.com/index.php?show=blog
directly returning content with "200 OK" status. You would need to solve that Duplicate Content scenario by redirecting those requests to
www.example.com/
too.

Whenever you redirect to a new URL, you should not include 'index.php' as a part of the new URL. The canonical URL is the shorter one ending with a slash.

That's why I believe you have your thoughts 'exactly backwards'.



So, as far as I can see, what you should be doing is this:

If user requests URL
(www.)example.com/index.html
or
(www.)example.com/index.htm
or
(www.)example.com/index.php
or URL
(www.)example.com/index.php?show=blog
they should be 301 redirected to the new URL
www.example.com/
. This bit is the redirect.

When user requests URL
www.example.com/
the content should be silently served from server internal filepath
/index.php?show=blog
without the URL bar in the browser being updated. This bit is the rewrite.


The left side of the RewriteRule is evaluated first. That pattern examines the path part of the URL request. It cannot see the domain name nor any query strings. After the left side is evaluated and a match found, the RewriteConds are examined. Here you can test values for HTTP_HOST, QUERY_STRING, THE_REQUEST, and others.

jdMorgan

1:40 pm on Apr 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I agree with the previous post. There is no reason to 'expose' the internal filepath and technology used by the server to the client as a URL.

But to clarify one point:

> The above gets into a loop.. but why? the url is rewritten into www.example.com/index.php so the RewriteCond should never match the new url

Yes it will, because only the requested hostname is tested by that rewritecond, and the hostname is "still" www.example.com -- The URL-path is not included in the value contained in HTTP_HOST. The new rule works because its rewriterule pattern is more specific, and will not match requests for "/index.php" -- it will match only requests for "/" -- so that is what prevents the 'infinite loop' that resulted from the original coding attempt.

Jim