This works fine:
https://example.com/2023/not-the-dinner-day
Using the following RewiteRule
RewriteRule ^(\d{4})/not-the-dinner-day/?$ archive/reports/$1/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 all sorts of things including:
RewriteEngine On
RewriteRule ^(\d{4})/not-the-dinner-day-photographs/?$ archive/galleries/$1/$2_ntdd/ [NC,L]
Matching the last folder 20230204_ntdd
seems to be causing me problems, rewrite module is turned on.
Since the 0204
(MMDD) part is not present in the requested URL you would need to hardcode code this. Fortunately, there is just one MMDD for any one year.
If you have access to the server config then you could implement a RewriteMap
that contains the one-to-one mapping - that can then be looked up in .htaccess
.
Otherwise, you could do something like the following in .htaccess
:
RewriteCond $1-0202 2019-(\d{4}) [OR]
RewriteCond $1-0201 2020-(\d{4}) [OR]
RewriteCond $1-0206 2021-(\d{4}) [OR]
RewriteCond $1-0205 2022-(\d{4}) [OR]
RewriteCond $1-0204 2023-(\d{4})
RewriteRule ^(\d{4})/not-the-dinner-day-photographs/?$ archive/galleries/$1/$1%1_ntdd/index.php [NC,L]
The preceding conditions contain the one-to-one mapping of YYYY to MMDD. $1
is the captured 4 digit year from the RewriteRule
pattern and the CondPattern then captures the corresponding MMDD from the TestString, which is retrieved using the %1
backreference in the resulting substitution string.
Aside:
However, there are a couple of potential SEO issues with your original rule:
You are allowing an optional trailing slash on the requested URL. But both would return the same resource. These are strictly two different URLs and potentially creates a duplicate content issue. If there is a possibility of both URLs (with and without a trailing slash) being received then you should ideally be redirecting from one to the other (whatever is canonical).
You are allowing a case-insensitive match. Again, this potentially creates a duplicate content issue. If you genuinely have mixed case requests then you should be canonicalising/correcting the request with an external redirect. See the following:
How to rewrite URLs from UPPERCASE to lowercase in .htaccess
If you are changing an existing URL structure (that has already been indexed and/or linked to by third parties) then you need to implement an external 301 redirect in the other directive, from old to new, in order to preserve SEO.