Welcome to WebmasterWorld Guest from 35.153.73.72

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

Problem with rewrite 301

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

Junior Member

5+ Year Member

joined:Dec 19, 2009
posts: 64
votes: 2


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]

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

Administrator from US 

WebmasterWorld Administrator not2easy is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 27, 2006
posts:4349
votes: 292


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.
7:03 am on Aug 14, 2017 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 19, 2009
posts: 64
votes: 2


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]

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

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts: 328
votes: 24


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.
8:01 am on Aug 14, 2017 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 19, 2009
posts: 64
votes: 2


Hi,

I only have a domain and "idpost" and "numberorder" are digits.
10:50 am on Aug 14, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11712
votes: 210


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]

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

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15699
votes: 810


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.
7:14 pm on Aug 14, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11712
votes: 210


Typo . in there?

yes it was and it's now been fixed...
8:13 pm on Aug 14, 2017 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 19, 2009
posts: 64
votes: 2


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.
11:22 pm on Aug 14, 2017 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts: 328
votes: 24


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.
11:39 pm on Aug 14, 2017 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 19, 2009
posts: 64
votes: 2


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
11:47 pm on Aug 14, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11712
votes: 210


Did you notice which directives were in /photos/.htaccess before it was deleted?
12:51 am on Aug 15, 2017 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 19, 2009
posts: 64
votes: 2


Yes, basically there was a redirect

RewriteCond %{HTTP_HOST} ^images.domain.com/$ 
RewriteRule ^(.*)$ https://www.domain.com/photos/$1 [R=301,L]
1:43 am on Aug 15, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15699
votes: 810


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.