homepage Welcome to WebmasterWorld Guest from 54.204.249.184
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

    
Changing URL
calvinmicklefinger




msg:4147568
 1:54 am on Jun 6, 2010 (gmt 0)

I apologize for asking a question that will be simple to most of you. But my experience with mod_rewrite is very limited, and I can't seem to understand the information I've been reading all day.

Anyhoo, I am trying to display a URL that is different from the filename requested by the link the user clicked.

My situation is that I have pages at [mysite.com...] and a page at [mysite.com...] and I want the display in the address bar to show a single consistent address of [mysite.com...] or [mysite.com...]

When I could not get the filename to disappear, I tried to go for the second option using this .htaccess code ...

RewriteEngine on
RewriteRule ^([0-9a-zA-z]*)\.php$ foo.php [L]


It dursen't work.

Can someone please provide me with the appropriate code. I am not a coder, but I have tried finding the information and reading all the tutorials and guides I have been able to find for the past twelve hours.

Even if you can't provide exact code, any help at all will be appreciated.

Kirk

 

g1smd




msg:4147603
 6:11 am on Jun 6, 2010 (gmt 0)

When the server rewrites the something.php URL request to the internal filepath foo.php, Mod_Rewrite starts again and foo.php also matches the pattern and is rewritten to foo.php, which matches the pattern and is rewritten to foo.php, which matches the pattern and it loops forever.

Add a negative match
RewriteCond $1 !^foo.php$ before the RewriteRule to fix this.

By the way, the pattern
^([0-9a-zA-z]*)\.php$ allows a URL request for example.com/.php to be valid. The * should be changed to + to prevent that happening.

However, I am not at all sure this code does what you want it to do, and I am not all that clear what it is that you actually want to do.

This code is an internal rewrite. It will accept a URL request for
example.com/<anything>.php and fetch the content from the file at foo.php. The address bar will continue to show the originally requested URL.
calvinmicklefinger




msg:4147680
 1:26 pm on Jun 6, 2010 (gmt 0)

Thanks for the debugging advice. I appreciate that.

This code is an internal rewrite. It will accept a URL request for example.com/<anything>.php and fetch the content from the file at foo.php. The address bar will continue to show the originally requested URL.


That's what I gotten, and is the opposite of what I would like to do.

I would like to fetch the content of the file but display the truncated URL. showing merely the domain name for all files, no matter what their full path is.

Kirk

g1smd




msg:4147710
 4:13 pm on Jun 6, 2010 (gmt 0)

That's not possible.

The full URL is contained in the request sent by the browser and appears in the URL bar of the browser at the point the browser makes that URL request to the server.

The browser must request both a domain name and a filepath and/or filename in order for the server to know exactly what content to return for that request.

It is possible for the displayed and requested URL to be
example.com/short and for an internal rewrite to connect that request to a /much-longer-path/and-file-name.php inside the server.
calvinmicklefinger




msg:4147759
 6:38 pm on Jun 6, 2010 (gmt 0)

I understand.

That is the conclusion I was coming to.

An alternative thought I had was to send all pages to the index page, with the content page identified as a parameter or variable. For example, immediately after the .com, I /?i-3. Then have the main page run through a php switch statement and insert the appropriate content. Then make a "pretty" url which would entail only removing the query string.

Reckon that would work?

Here's what I'm thinking so far. Create this link ...

<a href="http://mysite.com/?i=2">Click Here</a>

and on the index.php page ...

<?php
switch ($i) {
case 0:
include page1.htm;
case 1:
include page2.htm;
case 2:
include page3.htm;
}
?>

Then figure out the .htaccess to make a "pretty" url.

Could that work?

g1smd




msg:4147797
 8:18 pm on Jun 6, 2010 (gmt 0)

Yes, that would work, but - and this is the kicker - just because you have a script at
/index.php and calling it with parameters like /index.php?p=234 displays the various pages, there is no need for the /index.php or the ?p=234 part to appear in the URL in *that* form.

Indeed, you're better off changing the HTML code so that site links to, and uses, URLs like
example.com/p234 and then also re-arranging the rewrite code so that the rewrite internally calls the script at /index.php?page=234 without revealing that location to the outside world.

DirectoryIndex index.php
RewriteEngine On

# Rewrite URL request for example.com/p<num> to script at /index.php?page=<num>
RewriteRule ^p([0-9]+)$ /index.php?page=$1 [L]


The good news is that there are literally thousands of prior threads with similar requirements and
.htaccess code examples right here in this forum.

Finally, your script will return the numbered page requested. It will return the "index" page when no page number is specified. When a non-valid page number is requested, the script MUST return the HTTP 404 header, and an error message. This last point is VITAL.

calvinmicklefinger




msg:4147835
 9:48 pm on Jun 6, 2010 (gmt 0)

Thanks Bunches!

This is cool. (I'm not a coder, and this stuff always impresses me.)

Kirk

g1smd




msg:4147860
 10:53 pm on Jun 6, 2010 (gmt 0)

<?php
switch ($i) {
case 0:
include page1.htm;
case 1:
include page2.htm;
case 2:
include page3.htm;
}
?>


Why make case 1 fetch page 2?

Simplify the code to two lines, with case x fetching page x.

Then figure out the .htaccess to make a "pretty" url.

The .htaccess can't ever "make" a URL. URLs are defined in the links that you publish on your pages. The .htaccess code can accept that URL request and then go fetch the content from a non-default location inside the server.

calvinmicklefinger




msg:4147889
 12:17 am on Jun 7, 2010 (gmt 0)

This is what I wound up with. It seemed to simplify a lot of things and handles a lot of different situations. Please remember, I aren't no coder, so I hope it is bug proof. Please advise if it amn't.

In my .htaccess ...
DirectoryIndex index.php
RewriteEngine On
# Rewrite URL request for example.com/p<num> to script at /index.php?page=<num>
RewriteRule ^([A-Za-z0-9]+)$ /index.php?i=$1 [L]


In my index.php ...
<?php
$var = strip_tags($_GET['i']);
if (file_exists("$var.php"))
{
include "$var.php";
}Else{
include '0.php';
}
?>


Seems to work so far, and handles malformed or missing pages if the file is missing.

What you think?

g1smd




msg:4147984
 6:34 am on Jun 7, 2010 (gmt 0)

It looks good so far. I assume
0.php includes:

<?php HEADER "HTTP/1.1 404 Not Found"; ?>

or similar.

I don't see how the code supplies a page for "example.com/" requests. That is when "i" is blank. You'll need to test for that.

calvinmicklefinger




msg:4148104
 12:17 pm on Jun 7, 2010 (gmt 0)

It looks good so far. I assume 0.php includes:

<?php HEADER "HTTP/1.1 404 Not Found"; ?>

or similar.

0.php supplies the content that would be in index.php. I did that in lieu of a 404 as this site is nothing more than landing pages and squeeze pages.

I don't see how the code supplies a page for "example.com/" requests. That is when "i" is blank. You'll need to test for that.

In all instances, if there is no file "i," then 0.php is served as an include. 0.php is a generic squeeze page that pretty much says "Subscribe to my list." It appears to serve that include when "mysite.com" is called.

Thanks for your help in this, you were invaluable.
Kirk

jdMorgan




msg:4148143
 2:18 pm on Jun 7, 2010 (gmt 0)

> In all instances, if there is no file "i," then 0.php is served as an include.

Then that is *massive* duplicate-content -- Or to put in anther way, potential "SEO suicide."
It's simply not "good enough" -- There's no such thing, in fact.
No short-cuts. Watch the details, follow the HTTP specifications.
Serve a proper 404 response if you want to survive...
See the old thread in the Google forum "Duplicate Content - Get it right or perish." Great title.

Otherwise, go ahead and start typing your "Help, my site has been completely removed from Google" post -- you may need it sooner,rather than later.

Mod_rewrite entails complex directives and regular-expressions. It affects your server operation and the 'view' that browsers and search engines take of your site. As such, every little detail counts. Your previous respondent in this thread is well-aware of both the code-related issues and the SEO-related issues. I suggest that you not ignore even the tiniest suggestion that's been posted...

Jim

calvinmicklefinger




msg:4148203
 4:07 pm on Jun 7, 2010 (gmt 0)

I suggest that you not ignore even the tiniest suggestion that's been posted...

I have tried not to. What did I miss?

These pages are not planned as SEO fodder, but as landing pages for links included in an email campaign.

My main objective is to have a single page that appears if the visitor does not know the exact page they are looking for. For example, if I send a special offer to folks on my email list, I do not want others to easily find that offer. Else, the folks on the list will not see the specialness (is that a word) of the offer.

I'm open to suggestions, while you and this site have been invaluable over the years, I sometimes have trouble following the thought processes of others without having it explained to me several times. For that I apologize.

I would appreciate help in identifying what I have misunderstood, or just plain not understood.

Kirk

g1smd




msg:4148409
 9:05 pm on Jun 7, 2010 (gmt 0)

What did I miss?

This...

Your script will return the numbered page requested. It will return the "index" page when no page number is specified. When a non-valid page number is requested, the script MUST return the HTTP 404 header, and an error message. This last point is VITAL.

... and ...

I don't see how the code supplies a page for "example.com/" requests. That is when "i" is blank. You'll need to test for that.

So, you send the index page when "i" is blank, the correct numbered page if it exists, and the 404 HEADER and error message when a page does not exist.

Don't panic about having trouble understanding this stuff. It took me several years to get my head round it all.

calvinmicklefinger




msg:4148422
 9:53 pm on Jun 7, 2010 (gmt 0)

Thanks,

So, you send the index page when "i" is blank, the correct numbered page if it exists, and the 404 HEADER and error message when a page does not exist.

The index.php is the page that contains the script. All "pages" are loaded into index.php as php "includes." Right now the script pulls 0.php as an include whenever a file does not exist. So, my goal was to have a person either go to the page they were sent to, or the index page. None other.

I have been thinking of this as a "custom" 404 page, but have not been sending any 404 header information, because I did not think it applicable. (Well, really, because I don't know how.)

If a casual visitor starts poking around, I want them to have the "opportunity" to subscribe to my list on the index page (which is an include of 0.php). Otherwise, the visitor will go to the page their link targeted.

How can I meet that desire and avoid being de-indexed (even though I'm not going to e doing much, if anything, in the way of SEO for this site)?

g1smd




msg:4148440
 10:36 pm on Jun 7, 2010 (gmt 0)

OK, serve the file 0.php for URL requests for "/".

The way to send the 404 header is simple, and is shown in post #4147984 above.

You just send that header from within your index.php file, and follow it with:

<!DOCTYPE ....>
<html>
<head><title>Page Not Found</title></head>
<body>Error. Page Not Found.</body>
</html>


but obviously with a lot more text than that very simple example.

calvinmicklefinger




msg:4148459
 11:27 pm on Jun 7, 2010 (gmt 0)

Thanks.
I appreciate the help!

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