Ad

.htaccess SEO Dynamic Urls Not Working

- 1 answer

I have a page with a link with a query string that loads another page with the result of the query.

a target="_blank" rel="nofollow noreferrer" href="news-detail.php?nrf=<?php echo $ntitle; ?>"><?php echo "<img src=newsimgs/thumbs/".$nphoto1.">" ?></a>

Now when the new-detail page loads, i get the url as

https://www.xyzee.org/news-detail.php?nrf=Testing

But i want it to load as

https://www.xyzee.org/news-detail/Testing

I have looked online for tutorials on mod_rewrite and tried different samples but for reasons beyond my understanding, i cant get it to work. one of the sample code i have tried is

<IfModule mod_rewrite.c>

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^news-detail/([A-Za-z0-9-]+) news-detail.php?nrf=$1 [NC]

</IfModule>

This should work when i click the link that loads the details page but it dose not. The page still loads with the link as https://www.xyzee.org/news-detail.php?nrf=Testing

When i type directly in the browser, https://www.xyzee.org/news-detail/Testing, the page actually loads but the links to the css, js, images, etc, are all broken. I have tried so many ways to resolve this but cant seem to find a solution. Is it the link with the query string that is the problem or is it something else, i just don't know. Any help would be much appreciated. Thanks in advance.

Ad

Answer

Using mod_rewrite doesn't replace the links inside your documents. You have to do this inside your own code:

<a target="_blank" rel="nofollow noreferrer" href="news-detail/<?php echo $ntitle; ?>"><?php echo "<img src=newsimgs/thumbs/".$nphoto1.">" ?></a>

After that, you must also update the paths to your external resources, as the called page is now in a sub-directory. So you could append ../ to all included client side resouces inside the detail page.

To prevent this, the better way would be to use absolute paths or add the <base /> to the page's header.

Ad
source: stackoverflow.com
Ad