1

I'm looking to create a .htaccess URL redirect for a specific URL.

Redirect /photography/index.php https://www.example.com/photography-services/

but the issue is, I need URL patterns like:

https://www.example.com/photography/index.php?view=findphotos

to still work.

Basically, any index.php URL with a variable to still go to the page, but anyone trying to attempt to reach the landing page directly would get redirected to a new page. Is that possible?

1 Answer 1

0

Yes, this is possible...

In other words, you only want the specific URL /photography/index.php (without a query string) to redirect to /photography-services/.

You need to use mod_rewrite to check for an empty query string.

Try the following at the top of your .htaccess file in the document root.

RewriteEngine On

RewriteCond %{QUERY_STRING} ^$
RewriteRule ^photography/index\.php$ /photography-services/ [R=302,L]

If you have existing mod_alias Redirect (or RedirectMatch) directives then you may need to also convert these to mod_rewrite, if there is a conflict.

Note that the above is a 302 (temporary) redirect. Only change it to 301 (permanent) - if that is the intention - once you have confirmed that it works OK (to avoid caching issues).

You will need to clear your browser cache before testing.

3
  • One minor edit required: RewriteRule ^photography/index\.php$ /photography-services/ [R=302,L] --> RewriteRule ^/photography/index\.php$ /photography-services/ [R=302,L]
    – Thomas Kay
    Dec 13, 2018 at 2:56
  • But that "minor edit" won't work in .htaccess? The URL-path that the RewriteRule matches against does not start with a slash in a directory context.
    – MrWhite
    Dec 13, 2018 at 3:02
  • ... you would only require a slash prefix if this directive was in a server or virtualhost context.
    – MrWhite
    Dec 13, 2018 at 10:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .