Link to home
Start Free TrialLog in
Avatar of rbudj
rbudjFlag for United States of America

asked on

mod_rewrite Need Solution

In reference to my previous post: https://www.experts-exchange.com/questions/26765898/Rewrite-URL.html?anchorAnswerId=34882686#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.


Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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.

Avatar of rbudj

ASKER

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.
Just use the POST form's method instead of GET to send the variables, and they won't shown on the url.
Just curious... Why do you not want to show the resolved URL with the id?  Is there some perceived advantage to hiding this information?
And of course use mod_rewrite to show whatever name you want. The variable should be available inside the $_POST map
Avatar of rbudj

ASKER

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.
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

SOLUTION
Avatar of JayDiablo
JayDiablo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rbudj

ASKER

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.
Avatar of rbudj

ASKER

I wish experts-exchange would allow editing.   I meant to say,"The point is to HAVE the url show www.mydomain.com/Page-Name."
Avatar of rbudj

ASKER

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

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.
Avatar of rbudj

ASKER

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.

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.
Avatar of rbudj

ASKER

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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of rbudj

ASKER

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

Avatar of rbudj

ASKER

I found my solution for this. I am gathering info so I can post it and award points. Thanks to everyone.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rbudj

ASKER

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.