Forum Moderators: phranque

Message Too Old, No Replies

Problem with rewrite 301

         

chms

5:39 am on Aug 14, 2017 (gmt 0)

10+ Year Member



Hi,

I have a redirect 301 but it doesn't work and I don't know why, can you help me?

RewriteCond %{HTTP_HOST} ^www.example.com/photos/$
RewriteRule ^(.*)_(.*)\.jpg$ https://www.example.com/photos/$1/$1_$2.jpg [R=301,L]

I also have tried with the next but nothing:

RedirectMatch 301 ^photos/(.*)_(.*).jpg$ https://www.example.com/photos/$1/$1_$2.jpg

Thanks in advance.


[edited by: not2easy at 5:53 am (utc) on Aug 14, 2017]
[edit reason] used 'example.com' for readability [/edit]

not2easy

6:09 am on Aug 14, 2017 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



First, you shouldn't mix RewriteRule (Mod_Rewrite) with RedirectMatch (uses Mod_Alias) as that can have unexpected results.

It helps a lot if we start off by understanding what existing files you want to change, and from what URL/filename format to what result. For example, if you have /photos/photo_name.jpg (as it appears you have) and would like to redirect those requests to /photos/photo/photo_name.jpg (as it appears you would like). With clear objectives better assistance can be offered.

A very similar situation is discussed here: [webmasterworld.com...] that may help in the meantime.

chms

7:03 am on Aug 14, 2017 (gmt 0)

10+ Year Member



Hi not2easy,

Thanks for your response.

I have move my photos inside folders because i had a lot mixed in a folder and now I put inside a folder named like his post.

Before the photos was in https://example.com/photos/idpost_numberorder.jpg
Now are in https://example.com/photos/idpost/idpost_numberoforder.jpg

I'm going to take a look to that post.

Thanks

[edited by: phranque at 10:40 am (utc) on Aug 14, 2017]
[edit reason] exemplified urls for clarity (example.com isn't linked in this forum) [/edit]

whitespace

7:48 am on Aug 14, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



RewriteCond %{HTTP_HOST} ^www.example.com/photos/$


This would never match because the HTTP_HOST server variable contains the requested hostname only, not the hostname + (part of the) URL-path.

But do you have multiple domains? If not, then you don't need to check THE_HOST.

RedirectMatch 301 ^photos/(.*)_(.*).jpg$ https://www.example.com/photos/$1/$1_$2.jpg


This would never match because the RedirectMatch directive matches against the URL-path starting with a slash (unlike the mod_rewrite RewriteRule directive that omits the slash prefix in a directory context).

But, as not2easy suggests, which method you use (mod_rewrite or mod_alias) can depend on the existing directives you have in your .htaccess file.

/photos/idpost_numberorder.jpg


Ideally, you should be as specific as possible with your regex. For example, if "idpost" and/or "numberorder" are really numbers/digits, then match only digits, rather than anything.

But otherwise, it looks like you almost have it.

chms

8:01 am on Aug 14, 2017 (gmt 0)

10+ Year Member



Hi,

I only have a domain and "idpost" and "numberorder" are digits.

phranque

10:50 am on Aug 14, 2017 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



i would try something like this:
RewriteRule ^photos/(\d+)_(\d+)\.jpg$ https://www.example.com/photos/$1/$1_$2.jpg [R=301,L]

[edited by: phranque at 7:13 pm (utc) on Aug 14, 2017]

lucy24

3:50 pm on Aug 14, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



photos/(\d+)_(.\d+)\.jpg

Typo . in there? (Non-lethal typo, if so, unless this element is sometimes only one digit.)

It took a little head-scratching, but I think I see what you're fundamentally doing--dividing a bunch of existing image files into multiple directories according to category--and it makes sense.

phranque

7:14 pm on Aug 14, 2017 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Typo . in there?

yes it was and it's now been fixed...

chms

8:13 pm on Aug 14, 2017 (gmt 0)

10+ Year Member



Hi and thanks to everone,

this doesn't work, nothing happens:
RewriteRule ^photos/(\d+)_(\d+)\.jpg$ https://www.example.com/photos/$1/$1_$2.jpg [R=301,L]

I also I have tried with:
RewriteRule ^photos/([0-9]+)_([0-9]+)\.jpg$ https://www.example.com/photos/$1/$1_$2.jpg [R=301,L]
but it does not work either.

lucy24 you are right, I had also one folder with 500,000 photos, imposible works with it, syply list the folder was terrible, I have moved all photos to subfolders named with the id of the post, this is a real URL example:


Before -> https ://www.domain.com/photos/1171300_1.jpg
Before -> https ://www.domain.com/photos/1171300_2.jpg
...
Now -> https ://www.domain.com/photos/1171300/1171300_1.jpg
Now -> https ://www.domain.com/photos/1171300/1171300_2.jpg


I do not know what else to try.

whitespace

11:22 pm on Aug 14, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



Those two directives are equivalent and look as if they should work. Presumably, you have other mod_rewrite directives that are working?

You may have a conflict with existing directives - the order is usually important. Please include the contents of your .htaccess file so we can check.

chms

11:39 pm on Aug 14, 2017 (gmt 0)

10+ Year Member



Hi whitespace,

Just now I was seeing that, if there was another .htaccess in folder /photos and yes, that was the problem, I've deleted it and now works.

I was making crazy because I've check al combinatios and it shoud work but nothing.

Finally I'm using:

RewriteRule ^photos/([0-9]+)_([0-9]+)\.jpg$ https://www.domain.com/photos/$1/$1_$2.jpg [R=301,L]


Also this works

RewriteRule ^photos/(\d+)_(\d+)\.jpg$ https://www.domain.com/photos/$1/$1_$2.jpg [R=301,L]


Thaks for your help

phranque

11:47 pm on Aug 14, 2017 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Did you notice which directives were in /photos/.htaccess before it was deleted?

chms

12:51 am on Aug 15, 2017 (gmt 0)

10+ Year Member



Yes, basically there was a redirect

RewriteCond %{HTTP_HOST} ^images.domain.com/$ 
RewriteRule ^(.*)$ https://www.domain.com/photos/$1 [R=301,L]

lucy24

1:43 am on Aug 15, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Bingo. By default, mod_rewrite does not inherit, so the moment you have RewriteRules in more than one htaccess along the same path, the first set of RewriteRules are ignored as if they never existed.