Forum Moderators: phranque

Message Too Old, No Replies

Redirect to specific php

         

RonaldoL

3:54 am on Oct 17, 2011 (gmt 0)

10+ Year Member



Hi,

I want to redirect some URL's to specific php programs or html pages. All others URL's, that aren't specified will be redirect to the same php program. I'm using these htaccess lines.

RewriteEngine On
RewriteBase /
RewriteRule ^.*loadpage\.html$ /adm/read_header\.php [L]
RewriteRule ^.*pag01\.html$ /adm/page01\.html [L]
RewriteRule ^.*pag02\.html$ /adm/page02\.html [L]
RewriteRule ^.*pag03\.html$ /adm/page03\.html [L]
RewriteRule ^(.*)+$ /adm/rmenezes/not_defined\.php [L]

What are wrong, the error that I'm getting are:

500 internal server error - The server encountered an internal error or misconfiguration and was unable to complete your request.


Thanks and regards,

Ronaldo

g1smd

6:53 am on Oct 17, 2011 (gmt 0)

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



NEVER use .* at the beginning of a RegEx pattern. It is only ever valid at the END of a pattern.

Do not escape literal periods in the rule target. DO escape them in the RegEx pattern.

You asked for an external redirect and supplied code for an internal rewrite.

Which one do you want?

To explain your question in more detail you need to specify which of these is the URL path used on the web and which is the internal server path where the content really resides.

loadpage.html
/adm/read_header.php

Use this wording for a redirect:
When user requests the URL example.com/thispath I want to redirect the user to the URL example.com/newpath and the browser address bar will show this new URL.

Use this wording for a rewrite:
When user requests the URL example.com/newpath I want to show the content that resides at the path /internalpath inside the server. The browser address bar will NOT change and will continue to show the originally requested URL.

URLs are used "out there" on the web. Filepaths and files are used "here" inside the server. They are not at all the same thing.

Be sure you fully understand the difference between a redirect and a rewrite. They both use RewriteRule but with only minor syntax changes.

A redirect uses the [R=301,L] flags and the rule target will contain protocol and domain name.

A rewrite will use only the [L] flag and the rule target will contain only an internal server filepath reference.

As coded now, your first four rules promote almost infinite duplicate content.

The last rule in your list results in an infinite rewrite loop.

However it is not clear from the question which is the URL you want to use out on the web and which is the place inside the server that the content actually resides at. Please clarify that using the exact wording style detailed above.

RonaldoL

10:27 am on Oct 17, 2011 (gmt 0)

10+ Year Member



Hi,

Thanks for your detailed explanation. I want to rewrite some URL's to specific php programs or html pages.

When user requests the URL example.com/loadpage.html I want to show the content that resides at the path /adm inside the server.

All others URL's, that aren't specified will be rewrited to the same php program. I'm using these htaccess lines.

RewriteEngine On
RewriteBase /
RewriteRule ^loadpage\.html$ /adm/read_header.php [L]
RewriteRule ^pag01\.html$ /adm/page01.html [L]
RewriteRule ^pag02\.html$ /adm/page02.html [L]
RewriteRule ^pag03\.html$ /adm/page03.html [L]
RewriteRule ^(.*)+$ /adm/not_defined.php [L]

The first four rules are working, but the last rule still doesn't rewrite for the correct php path and executing the script.

Thanks and regards,

Ronaldo

RonaldoL

2:34 pm on Oct 17, 2011 (gmt 0)

10+ Year Member



Hi,

I tried these commands, but I can't find the error.

RewriteEngine On
RewriteBase /
RewriteRule ^.*loadpage\.html$ /adm/read_header\.php [L]
RewriteRule ^.*pag01\.html$ /adm/page01\.html [L]
RewriteRule ^.*pag02\.html$ /adm/page02\.html [L]
RewriteRule ^.*pag03\.html$ /adm/page03\.html [L]
RewriteCond %{REQUEST_URI} !=adm/not_defined.php
RewriteRule ^(.*) adm/not_defined.php [L]

If, after made the rewrite, apache will submit the new request to the .htacces rules again, I test the URI to doesn't work if the URI are the php program. I'm correct? Why I'm getting an 500-Internal server error?

Regards

SteveWh

5:19 pm on Oct 17, 2011 (gmt 0)

10+ Year Member



This is probably what you intended for the last 2 lines. The ! does mean NOT, but you shouldn't use the = sign with it.

RewriteCond %{REQUEST_URI} !^/adm/not_defined\.php$
RewriteRule .* /adm/not_defined.php [L]

This will, however, return not_defined.php for every request that you haven't handled in the previous lines. Even for images, for example.

In your latest post, you went back to using the incorrect code. Your new code should look like this. Don't backslash periods in the destination URL:

RewriteEngine On
RewriteBase /
RewriteRule ^loadpage\.html$ /adm/read_header.php [L]
RewriteRule ^pag01\.html$ /adm/page01.html [L]
RewriteRule ^pag02\.html$ /adm/page02.html [L]
RewriteRule ^pag03\.html$ /adm/page03.html [L]

RewriteCond %{REQUEST_URI} !^/adm/not_defined\.php$
RewriteRule .* /adm/not_defined.php [L]

I'm still not sure this will do what you want, but the format should be closer to being correct.

RonaldoL

6:24 pm on Oct 17, 2011 (gmt 0)

10+ Year Member



Hi,

The first four RewriteRule aren't working, when I request for the loadpage.html or pag01.html or pag02.html or pag03.html. All RequestRule are redirects to "not_defined.php".

Seems to me that the [L] flag doesn't finish the rules execution. Where is the error?

Thanks for any help.

g1smd

6:52 pm on Oct 17, 2011 (gmt 0)

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



Do not rewrite infinite URLs to all serve the same content. It's a quick way to kill your site indexing.

You should redirect those requests to a single canonical URL, and then serve the content when that URL is requested. When the canonical URL is requested, you can serve the content either directly or using an internal rewrite.

You are still mixing up the words "redirect" and "rewrite". They are very different processes.

RonaldoL

7:39 pm on Oct 17, 2011 (gmt 0)

10+ Year Member



Hi,

What I'm trying to do are: a php program will receive all requests to this part of the site, except for some pages that will be under specific RewriteRules, not redirect.

Why the first four rules below aren't working when I tried to access some pre-defined URL? All request are accept for the RewriteCond/RewriteRule and rewrite to "not_defined.php"

RewriteRule ^loadpage\.html$ /adm/read_header.php [L]
RewriteRule ^pag01\.html$ /adm/page01.html [L]
RewriteRule ^pag02\.html$ /adm/page02.html [L]
RewriteRule ^pag03\.html$ /adm/page03.html [L]

RewriteCond %{REQUEST_URI} !^/adm/not_defined\.php$
RewriteRule .* /adm/not_defined.php [L]

Any help to work?

Thanks and regards

SteveWh

2:50 am on Oct 18, 2011 (gmt 0)

10+ Year Member



I'm unsure whether .htaccess is processed again after a Rewrite is done, but from what you describe, it sounds like it is. Therefore, each of your first four Rewrites is just ending up being processed by the last one (the second time around), and everything goes to not_defined. In that case, you need to create an exception for every page that you don't want rewritten:

RewriteRule ^loadpage\.html$ /adm/read_header.php [L]
RewriteRule ^pag01\.html$ /adm/page01.html [L]
RewriteRule ^pag02\.html$ /adm/page02.html [L]
RewriteRule ^pag03\.html$ /adm/page03.html [L]

RewriteCond %{REQUEST_URI} !^/adm/page01\.html$
RewriteCond %{REQUEST_URI} !^/adm/page02\.html$
RewriteCond %{REQUEST_URI} !^/adm/page03\.html$
RewriteCond %{REQUEST_URI} !^/adm/not_defined\.php$
RewriteRule .* /adm/not_defined.php [L]

If you want everything in /adm/ to get passed through and not be rewritten, this might do just as well, and is a bit simpler:

RewriteRule ^loadpage\.html$ /adm/read_header.php [L]
RewriteRule ^pag01\.html$ /adm/page01.html [L]
RewriteRule ^pag02\.html$ /adm/page02.html [L]
RewriteRule ^pag03\.html$ /adm/page03.html [L]

RewriteCond %{REQUEST_URI} !^/adm/
RewriteRule .* /adm/not_defined.php [L]

g1smd

6:33 am on Oct 18, 2011 (gmt 0)

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



While that would work, it creates infinite duplicate content.

The correct way to do this is to redirect non-canonical requests to a sinlge URL and for that URL to serve the content.

The rules for the numbered pages can also be simplified to a single rule too.

lucy24

7:35 am on Oct 18, 2011 (gmt 0)

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



I'm unsure whether .htaccess is processed again after a Rewrite is done

htaccess is processed over and over, as many times as necessary until it rinses clean. At which point mod_rewrite hands off to the next module in line.

The rules for the numbered pages can also be simplified to a single rule too.


Heh. I'd actually written that out before I figured out what you meant ;)

RewriteRule ^pag01\.html$ /adm/page01.html [L]
RewriteRule ^pag02\.html$ /adm/page02.html [L]
RewriteRule ^pag03\.html$ /adm/page03.html [L]


=

RewriteRule ^pag0([123])\.html$ /adm/page0$1.html [L]