0

I have made the following HTAccess 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 HTML and PHP code, I have these old URLS. I mean these URLs existed before I wrote the HTAccess file.

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

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

/private_gallery.php?id=SomeGalleryId

and not to

/private/SomeGalleryId

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

1 Answer 1

1

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]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I'll edit the code if it makes the SEO works better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.