Manitoba Manitoba - 10 months ago 56
Apache Configuration Question

URL Rewriting and PHP code

I have made the following

file for my personnal website (which is basically a portfolio).

# Error
ErrorDocument 401 /error.php?id=401
ErrorDocument 403 /error.php?id=403
ErrorDocument 404 /error.php?id=404
ErrorDocument 500 /error.php?id=500

# Enable URL rewriting
Options +FollowSymlinks
RewriteEngine on
RewriteOptions Inherit

# Url rewrite for assets
RewriteCond %{REQUEST_URI} !^/assets(/.*|)$ [NC]
RewriteCond %{REQUEST_URI} !^/gallery(/.*|)$ [NC]
RewriteRule ^.+$ / [R=302,L]

# Url rewrite for pages
RewriteRule ^home$ index.php
RewriteRule ^about$ about.php
RewriteRule ^gallery/(.+)$ gallery.php?id=$1
RewriteRule ^password/(.+)$ password.php?id=$1
RewriteRule ^private/(.+)$ private_gallery.php?id=$1
RewriteRule ^hidden/(.+)$ hidden_gallery.php?id=$1
RewriteRule ^download/(.+)$ download.php?id=$1
RewriteRule ^contact$ contact.php

This works great when I try to access these URLs directly through my web browser.

In my
code, I have these old URLS. I mean these URLs existed before I wrote the

<a href="private_gallery.php?id=SomeGalleryId">Open this gallery</a>

When the user clicks on this URL, he is redirected to


and not to


Is there a way to change that without editing my
code ?

Answer Source

You could use another rewrite to match on the query string and do a 301 redirect ... it would probably cause SEO experts to go cross-eyed; better to have the URLs in the form /private/SomeGalleryId in your HTML code and redirect invisibly to private_gallery.php?id=SomeGalleryId rather than this way around but...

RewriteCond %{REQUEST_URI} ^/private_gallery\.php$
RewriteCond %{QUERY_STRING} id=(\w+).*
RewriteRule ^.*$ /private/%1 [R=301,L]