php - RewriteRule only for specific pattern if 404 error would occur -
i'm trying update .htaccess-file goal, pages have responded "404 file not found"-error rewritten url.
example: i've got 3 links:
404: domain.com/category1/123-article-name.html
working: domain.com/static-page/10-ways-to-destroy-your-webpage.html
untouched: domain.com/simple-website.html
in case first link should redirected domain.com/lost/123-article-name.html, second 1 allready works. third link should never affected rule.
hope guys can me. that's hard nut crack, me! thank you!
edit:
rewriterule ^(.*)/([0-9]+)-(.+).html$ lost/$2-$3.html [l]
this i've got far, rule kind of "stupid", because not test wheater requested page exists or not.
your rule , rules cms mutually exclusive.
line 41 (your rule) execute if file exists, because if file doesn't exist (rewriteconds on line 38 , 39) server tries return index.php?page=path_name
generate 404 error.
if know names of categories/directories missing, per lines 10 35, can apply rule urls have them in it.
rewriterule ^(ausbildung|ausland|berufswelt)/(?=.+-)(.+).html$ lost/$2.html
original answer
you can test see if file or directory exists adding rewrite condition
rewritecond %{request_filename} !-f # true if request isn't file rewritecond %{request_filename} !-d # true if request isn't directory rewriterule ^.*/([0-9]+)-(.+).html$ lost/$2-$3.html [l] #not removed first capture group because unnecessary.
side note: in example, because +
greedy in regex, captured groups domain.com/category1/123-article-name.html
- 123-article
- name
which works, might artificially constrain you. change rule include positive ahead:
rewriterule ^.*/(?=.+-)(.+).html$ lost/$1.html
the (?=.+-)
tells regex engine create match number of characters if finds @ least 1 -
you should check out htaccess test, awesome!
Comments
Post a Comment