Solved

mod_rewrite Need Solution

Posted on 2011-02-13
21
286 Views
Last Modified: 2012-05-11
In reference to my previous post: http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_26765898.html#a34882686

I have been reading up on mod_rewrite... enough to be able to read and understand this:

# catch everything else (webpages) that don't exist in the filesystem
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ news_full.php?id=$1 [QSA]

Unfortunately I have run out of time and I need a completed .htaccess file.  I am using the $_GET on news_full.php to pull the id variable from the URL. I do not know what to do next. I need the part where mod_rewrite looks in the datatabase and pulls the PageName variable where id=id and displays it in the URL instead of the index.php?id=23.

The mySQL table:

id, PageName, NewsTitle, NewsArticle

Again I need a complete .htaccess file.... please. Thank you.


0
Comment
Question by:rbudj
  • 11
  • 6
  • 2
  • +1
21 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34882904
I may not be understanding your question, but I don't think mod_rewrite is the tool you need here.  

Inside new_full.php you will find a variable named $_GET["id"].  This variable contains the value of XXX in this URL string:
http://example.com/news_full.php?id=XXX

You can take that value of XXX (filter and escape as needed) and use it to perform a lookup in the data base.

0
 
LVL 16

Author Comment

by:rbudj
ID: 34883016
Yes. I could take the id variable and pull the PageName but how would I get that to display in the url.

I need this: www.mydomain.com/news_full.php?id=23

to read: this: www.mydomain.com/PageName

where PageName is in a database table along with the news article.
0
 
LVL 6

Expert Comment

by:t-max
ID: 34883105
Just use the POST form's method instead of GET to send the variables, and they won't shown on the url.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34883115
Just curious... Why do you not want to show the resolved URL with the id?  Is there some perceived advantage to hiding this information?
0
 
LVL 6

Expert Comment

by:t-max
ID: 34883121
And of course use mod_rewrite to show whatever name you want. The variable should be available inside the $_POST map
0
 
LVL 16

Author Comment

by:rbudj
ID: 34883153
Ray... I have to display the page name instead of the id variables because that is what my client wants. So, I'm stuck with coming up with the solution.

t-max, thanks for the info but as stated, I need a complete file. I'm out of research time and I have to get this done asap. Again:

I need this: www.mydomain.com/news_full.php?id=23

to read: this: www.mydomain.com/PageName

where PageName is in a database table along with the news article.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34883197
Interesting... I actually have gone the other way here (click and see it redirect to the correct PHP url).  The code snippet shows how that works.  
http://www.landonbaseball.com/coaches

It looks like you almost want an opposite strategy.  Here is an idea.  I am not sure that it will work and I do not have time to test it right now, but the strategy seems good enough that it could be worth trying.

You will give the public a link that says something like http://www.landonbaseball.com/coaches and there will be no script named "coaches" so you will get a 404-not-found condition.  The 404 handler will look up "coaches" in the data base table and find the appropriate URL, which might be something like "news_full.php?id=23".  With that, you would set $_GET["id"] to "23" then require_once("news_full.php");  This would almost certainly simulate a direct link to "news_full.php?id=23" and since the superglobal array $_GET is not immutable, you can change it.

In your example, you show id=23 and PageName.  I think an interesting question might be, "How do these two pieces of information get linked together?"
<?php // landonbaseball.com/404handler.php
error_reporting(E_ALL);

if (isset($_SERVER["REQUEST_URI"]))
{
    // IF A PHP SCRIPT IS NOT FOUND GO TO THE HOME PAGE
    if (preg_match('#\.php#i', $_SERVER["REQUEST_URI"]))
    {
        header('Location: /');
        exit;
    }

    // IF A NON-PHP SCRIPT IS NOT FOUND, TRY IT WITH PHP
    $arr = explode('?', $_SERVER["REQUEST_URI"]);
    $arr[0] .= '.php';
    $uri = implode('?', $arr);
    header("Location: $uri");
    exit;
}

// IF NO REQUEST URI
header('Location: /');
exit;

Open in new window

0
 
LVL 4

Assisted Solution

by:JayDiablo
JayDiablo earned 500 total points
ID: 34884409
I think it's still ok to use mod_rewrite here to redirect all requests to a single PHP file (if there's no matching file in the filesystem, which is what the mod_rewrite condition you have in your question does).

This is typically called the "Front Controller" design pattern, and a lot of PHP frameworks do exactly the same thing to route friendly URLs to various internal code structures.

Just modify the RewriteRule line to something like this:

RewriteRule ^.*$ news_feed.php [NC,L]

Open in new window


What this does is sends all requests that don't match a real file in the filesystem to news_feed.php.

Then, in news_feed.php you can look at the URL to determine what you want to do.  Here's an example of how to get the URL broken out into parts, that you can then use to display the appropriate content:

$urlParts = explode('/' , $_SERVER['REQUEST_URI']);

Open in new window


So, if your URL is "www.mydomain.com/PageName" you'll now have an array with 2 items, like so:

Array (
  '0' => 'www.mydomain.com',
  '1' => 'PageName'
)

So you could now theoretically goto your database and look for pages with the name of "PageName" (which would be stored in $urlParts[1]), if found, show the user that content, if not found, show a useful 404 error page or something like that.

As Ray mentioned briefly in a previous post, always be sure to validate and escape any parameters you send to your database, especially in this situation, since a user could type anything they want into the URL, and that will goto your database (which is perfect for SQL injection attacks).

You could also use this strategy to make urls like this:

http://www.domain.com/news/awesome-news-story-title

Again, this would be directed to your news_feed.php script, and now you'd have an array with three items instead of two.  Why would this be useful?  Perhaps you also want articles instead of just news.

http://www.domain.com/articles/sweet-title

Now you could build logic into news_feed.php to look at the part where "news" and "articles" are located, and look in a different SQL table, or something like that.
0
 
LVL 16

Author Comment

by:rbudj
ID: 34887929
Thank you Jay for your post. However, I still think everyone is mis-understanding me or something. You said, So, if your URL is "www.mydomain.com/PageName" you'll now have an array with 2 items, like so:

Array (
  '0' => 'www.mydomain.com',
  '1' => 'PageName'
)

The point is to the the url to show www.mydomain.com/PageName. If the URL says that then Im done. I do need to look more into using the dashes between words.  I wish I could provide you my information and you can provide me the code. I have tested and tested for two days and nothing is working.

I need this: www.mydomain.com/news_full.php?id=23

to read: this: www.mydomain.com/Page-Name

Database:

id, date, title, pageName, news_article

What other information can I provide that will help you help me? BTW... the code in my reference link in the opening of this question was just made by someone else. I do not know what it means and It does not work for me so the only thing I am trying to do is mentioned above. Still though no one has even mentioned putting this...

RewriteEngine on

...in the .htaccess file. Again I need a complete solution. Partial info will not help me close this project.  I am VERY thankful so far and the information will be valuable at a later date when I can actually sit down and learn mod_rewrite.
0
 
LVL 16

Author Comment

by:rbudj
ID: 34887946
I wish experts-exchange would allow editing.   I meant to say,"The point is to HAVE the url show www.mydomain.com/Page-Name."
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 16

Author Comment

by:rbudj
ID: 34887961
Actually, to be more precise, now that I know a little more about mod_rewrite:

I need this: www.mydomain.com/news_full.php?id=23&PageName=PageName

to read: this: www.mydomain.com/Page-Name

Database:

id, date, title, pageName, news_article

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34888065
Let's try to clarify this.

Please give us an example of the URL that you want your site visitor to type into the browser address bar.  Then show us what you want that URL to be translated into.
0
 
LVL 16

Author Comment

by:rbudj
ID: 34888335
Thank you Ray.

I have a news.php page that pulls news stories from the database and displays their date, and title. I have a Full Story link that points to news_full.php?id=23&pageName=MechanicalDryerOverHeats

... where id is the database id of the news article and pageName is the pageName of the article in the database. So of course each article on the news.php page will link to the news_full.php with its id and pageName variable being passed through the URL.

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34888377
One more time.  Let's try to clarify this.

Please give us an example of the URL that you want your site visitor to type into the browser address bar.  Then show us what you want that URL to be translated into.
0
 
LVL 16

Author Comment

by:rbudj
ID: 34888484
They type:

mydomain.com. They click a link on the home page that links to news.php. On news.php there is a link to the articles, example  news_full.php?id=23&pageName=MechanicalDryerOverHeats

So this is not a page that people will type in unless the URL has already been changed to a Clean URL.  

So the page:  news_full.php?id=23&pageName=MechanicalDryerOverHeats

Should be translated to: mydomain.com/pageName

but I will also like the dashes between words in pageName so translated to:

mydomain.com/page-name

Is this what you need?
0
 
LVL 4

Assisted Solution

by:JayDiablo
JayDiablo earned 500 total points
ID: 34888647
I think you're close to getting what you want, but it sounds like we haven't addressed one end of this.

The solutions that have been provided so far mainly deal with how your server is going to deal with urls like "domain.com/page-name" after the user has clicked on a link with that as the URL.

The other part of this is how do you provide the user with those sorts of links (which I think is what you're asking about now).

It's sort of difficult to provide a really useful example without seeing how you're currently doing it, but I suspect it looks something like this:

<a href="http://www.mydomain.com/news_feed.php?id=<?php echo $pageId; ?>&pageName=<?php echo $pageName; ?>">Link to article</a>

Open in new window


It really could be as simple as this:

<a href="http://www.mydomain.com/<?php echo $pageName; ?>">Link to article</a>

Open in new window


Using your example above, this would output:

<a href="http://www.mydomain.com/MechanicalDryerOverHeats">Link to article</a>

Open in new window


And if you have the mod_rewrite solution in place to deal with that, the user would be shown the "MechanicalDryerOverHeats" page's content (pulled from your database), and that's the URL they would see in their browser (and it's also the URL that google would index, etc...).

Is that what you're looking for?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34889242
Maybe you should build this site using CodeIgniter.  I think they offer something close to what you want (many frameworks do).

Not sure what to tell you about adding dashes to the URL strings.  You might want to make a Google search for "Apache Pretty URLs" and look at some of the examples there to see what is possible.  This offers one set of examples.
http://www.roscripts.com/Pretty_URLs_-_a_guide_to_URL_rewriting-168.html

best of luck with it, ~Ray
0
 
LVL 16

Author Comment

by:rbudj
ID: 34890895
Jay - That is exactly how the link is setup <a href="http://www.mydomain.com/news_feed.php?id=<?php echo $pageId; ?>&pageName=<?php echo $pageName; ?>">Link to article</a>

So an actual link when you move your cursor over it looks like - http://www.mydomain.com/news_full.php?id=23&pageName=MechanicalDryerOverHeats

0
 
LVL 16

Author Comment

by:rbudj
ID: 35039138
I found my solution for this. I am gathering info so I can post it and award points. Thanks to everyone.
0
 
LVL 16

Accepted Solution

by:
rbudj earned 0 total points
ID: 35157722
This is my solution:

Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteRule ^news_full/([^/]*)/?$ news_full.php?pageName=$1 [NC,L]

One of the problems with my question is that I did not know how to ask the question because of my lack of understanding on the subject. I was thinking that because the true link was to example: www.mydomain.com/news_full.php?id=23&pageName=MechanicalDryerOverHeats that I was supposed to make my <a href=""> point to that string. Instead, once I understood a little more, I determined that I'm actually supposed to make my link to be what the mod_rewrite was going to make it look like. So making my link <a href="news_full/MechanicalDryerOverHeats/">more news</a> solved the problem. Thanks to all who helped.
0
 
LVL 16

Author Closing Comment

by:rbudj
ID: 35187311
I did indeed want to use mod_rewrite instead of php. I needed a complete code which was partially provided. I have posed my complete .htaccess code. Your suggestions made more sense after I found my solution. Thank you.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now