This works fine:
https://example.com/2023/not-the-dinner-day
Use the following RewiteRule
RewriteRule ^(\d{4})/not-the-dinner-day/?$ archive/reports//ntdd.php [NC,L]
I want to get this new URL:
https://example.com/2023/not-the-dinner-day-photographs
From this old URL
https://example.com/archive/galleries/2023/20230204_ntdd/
I have tried various methods including:
RewriteEngine On RewriteRule ^(\d{4})/not-the-dinner-day-photographs/?$ archive/galleries//_ntdd/ [NC,L]
Match the last folder20230204_ntdd
seems to be causing the problem for me, the rewrite module is turned on.
Since the
0204
(MMDD) part does not exist in the requested URL, you need to hardcode it. Fortunately, there is only one MMDD in any given year.If you have access to the server configuration, you can implement a
RewriteMap
that contains a one-to-one mapping - that mapping can then be looked up in.htaccess
.Otherwise, you can do something like the following in
.htaccess
:The preceding conditions contain a one-to-one mapping of YYYY to MMDD.
$1
is the 4 digit year captured fromRewriteRule
pattern, then CondPattern is captured from TestString, which is used The%1
backreference in the generated replacement string is retrieved.Narration:
However, there are some potential SEO issues with your original rules:
You allow an optional trailing slash on the requested URL. But both will return the same resource. These are strictly two different URLs, and may create duplicate content issues. If it's possible to receive two URLs at the same time (with or without a trailing slash), then ideally you should redirect from one URL to the other (regardless of specification).
You allow case-insensitive matching. Again, this may cause duplicate content issues. If you do have mixed case requests, then you should use external redirects to normalize/correct the requests. Please see the following:
How to rewrite a URL from uppercase to lowercase in .htaccess
If you are changing the existing URL structure (that has been indexed and/or linked to by a third party), then you need to implement an external 301 redirect in another directive, from old to new, in order to protect searches Engine optimization.