Forum Moderators: phranque
Would I also be right in saying, that if my viewArticle.php page contained forms, and links to it in the format viewArticle.php?id=x that the code would need to be re written in the format article/x, or does Apache take care of this, so if I didn't change to code, so say for example in article/34, I clicked on submit, where it's action was viewArticle.php?id=34, the url the visitor would see would remain article/34?
The URL is defined by links appearing on your pages (and the pages of other Web sites). Apache mod_rewrite cannot change your page content, all it can do is to 'map' incoming requests for URLs (coming as the result of a click on one of your links) to the correct files on your server -- In this case, take a request for "example.com/article/x" and map it to "/viewArticle.php?id=x".
This is generally not hard to do, requiring only one line to be added to the code that generates your on-page links. Alternatively, you could mass-edit the database, so that it contains the new static URLs, and let the script just output them.
For more information, see this thread: Changing Dynamic URLs to Static URLs [webmasterworld.com]. There are additional tutorials in our forum library, and links to resources in our forum charter.
Jim
I have created a .htaccess in the folder with the viewArticle.php file, and using a rather simple rule did this:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^viewArticle.php?id=([0-9]+)$ /article/$1/ [R]
It doesn't work, like [another] article said it would. (I viewed yours, but I found this a tad easier to understand). I'm not understanding the logic of it all, is what I think the problem is.
[edited by: jdMorgan at 6:38 pm (utc) on June 29, 2008]
[edit reason] No URLs, please. See Terms of Service. [/edit]
That is not at all what I meant. The rule attempts to externally redirect from a dynamic URL to a static URL, when what in fact is needed is to change the links on the page to static URLs, and then internally rewrite from these new static link URLs to the dynamic filepath needed to invoke the script(s).
Notwithstanding the facts that the rule won't work as written because RewriteRule cannot 'see' query strings, and that it attempts to generate an external redirect, the rule is backwards, in that it attempts to redirect from dynamic to static. Since the static URL will never resolve to an existing file, it cannot work.
Jim
# Enable mod_rewrite, start rewrite engine
Options +FollowSymLinks
RewriteEngine on
#
# Internally rewrite search engine friendly static URL to dynamic filepath and query
RewriteRule ^article/([^/]+)/?$ /viewArticle.php?id=$1 [L]
#
# Externally redirect client requests for old dynamic URLs to equivalent new static URLs
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /viewArticle\.php\?id=([^&]+)\ HTTP/
RewriteRule ^viewArticle\.php$ /article/%1/? [R=301,L]
And it still won't work. Any ideas?
Also, comment-out the second rule (or delete it) until you get the first rule working.
What you have there actually looks OK, except the the substitution URL in the second rule (only) should start with "http://" and your domain name. However, that's not the likely coause of your trouble.
Please describe how you tested and what your results were. Then state how those results differed from your expectations.
Also be sure to completely flush your browser cache after any change to your config code.
Jim
Sorry, I don't understand "I still get directed" at all.
By "can see viewArticle.php?id=x", do you mean that you see that page's contents, that URL in your browser address bar, or?
Did you change the link on your page to /article/x? Did you flush your cache before testing?
Jim
The problem was actually the / in front of viewArticle
Now, though, I am in a subdomain, and down two folders deep, and ofcourse my index link, or stylesheet link (in an SSI) don't work (due to the wrong filepath). Is there a way I can correct these links, without damaging them on other pages getting them called on the SSI?