Mobile app version of vmapp.org
Login or Join
Hamaas447

: How can I redirect all files in a directory that doesn't conform to a certain filename structure? I have a website where a previous developer had updated several webpages. The issue is that

@Hamaas447

Posted in: #301Redirect #Htaccess #ModRewrite

I have a website where a previous developer had updated several webpages. The issue is that the developer had made each new webpage with new filenames, and deleted the old filenames. I've worked with .htaccess redirects for a few months now, and have some understanding of the usage, however, I am stumped with this task.

The old pages were named like so:
domain.tld/subdir/file.html

The new pages are named:
domain.tld/subdir/file-new-name.html

The first word of all new files is the exact name of the old file, and all new files have the same last 2 words.
domain.tld/subdir/file1-new-name.html www.domain.tld/subdir/file2-new-name.html domain.tld/subdir/file3-new-name.html ect.


We also need to be able to access the url:
domain.tld/subdir/

The new files have been indexed by google (the old urls cause 404s, and need redirected to the new so that google will be friendly), and the client wants to keep the new filenames as they are more descriptive. I've attempted to redirect it in many different ways without success, but I'll show the one that stumps me the most

RewriteBase /
RewriteCond %{THE_REQUEST} !^subdir/.*-new-name.html
RewriteCond %{THE_REQUEST} !^subdir/$
RewriteRule ^subdir/(.*).html$ www.domain.tld/subdir/-new-name.html [R=301,NC]


When visiting domain.tld/subdir/file1.html in the browser, this causes a 403 Forbidden error with a url like so:
domain.tld/subdir/file1-new-name-new-name-new-name-new-name-new-name-new-name-new-name-new-name-new-name-new-name-new-name-new-name-new-name.html

I'm certain it's probably something simple that I'm overlooking, can someone please help me get a proper redirect? Thanks so much in advance!

EDIT

I've also got all the old filenames saved on a separate document in case I need them set up like the following example:

(file(1|2|3|4|5)|page(1|2|3|4|5)|a(l(l|lowed|ter)|ccept)

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Hamaas447

1 Comments

Sorted by latest first Latest Oldest Best

 

@Jessie594

You need to remove the carats from your RewriteCond lines:

RewriteBase /
RewriteCond %{THE_REQUEST} !subdir/.*-new-name.html
RewriteRule ^subdir/(.*).html$ www.domain.tld/subdir/-new-name.html [R=301,NC]


The rewrite conditions will then take effect and you'll avoid the infinite redirect loop. This is required because your old rewrite condition will never take effect.

Your original RewriteCond %{THE_REQUEST} !^subdir/.*-new-name.html rule says "If the request does not start with the string "subdir...". But %{THE_REQUEST} variable contains the full HTTP request, which looks like "GET /subdir/file.html HTTP/1.1". It will never begin with "subdir", because it always begins with "GET".

One alternative would be to use %{REQUEST_URI} with a leading slash for the subdirectory name:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/subdir/.*-new-name.html
RewriteCond %{REQUEST_URI} !^/subdir/$
RewriteRule ^subdir/(.*).html$ todoodlist.com/subdir/-new-name.html [NC,R=301]

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme