wrong redirect in firefox with htaccess - .htaccess

There is website with two different pages with photos:
site.com/photo1.php and site.com/photo2.php; both pages are redirected by htaccess in Search Engine Friendly URLs, so site.com/photo.php?date=180322&name=event transforms to site.com/photo/180322/event/
there are two snippets from htaccess, but the first one doesn't work in firefox. In chrome works good
# photo1.php - redirect (1st - NOT workable in FF)
RewriteRule ^photo1/([0-9]{6})/([a-z]+)/?$ /photo1.php?date=$1&name=$2&norec [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} date=([0-9]{6})&name=([a-z]+)$
RewriteRule ^photo1.php$ /photo1/%1/%2/? [L,R=301]
# photo2.php - redirect (2st - workable in FF)
RewriteRule ^photo2/([0-9]{6})/([a-z]+)/?$ /photo2.php?date=$1&name=$2&norec [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} date=([0-9]{6})&name=([a-z]+)$
RewriteRule ^photo2.php$ /photo2/%1/%2/? [L,R=301]
snippets are identical, but firefox at the 1st variant redirects to site.com/photo1/.php/.php/.php/.php/.php/.php/.php/.php/.php/.php/.php/.php/.php/.php/.php/.php/.php/.php/
How should I solve this problem with FF?

I have solved the problem. I had to switch off Cashing in Preferences-Network. Now redirecting works as I wanted.

Related

htaccess redirect subdomain to directory

I need help to write proper rewrite rules in my htaccess files.
I need to redirect something like fr.example.com to example.com/fr, because we recently changed the whole website and the multilingual system is managed differently. The structure and the pages too.
I managed to do that successfully with this piece of code:
RewriteCond %{HTTP_HOST} ^fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
My problem now is to write something more specific for pages, for example :
fr.example.com/discover/foo should go to example.com/fr/bar/foo (different path, nothing consistant)
BUT ! example.com/discover/foo should go to example.com/bar/foo (end of the url is the same in both english and french)
Right now, since I have some common 301 redirects, the french urls aren't redirect properly and lead to the english pages. For example that one :
Redirect 301 /discover/foo /bar/otherfoo
Successfully redirects example.com/discover/foo to example.com/bar/otherfoo but also redirects fr.example.com/discover/otherfoo
How can I write two different rules for english and french? I'll have to write a bunch of different rules since everything is very different from the old subdomain to the new directory, I don't mind.
Thanks !
EDIT
Please note that it's for a wordpress installation, and the htaccess starts with :
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
First the these rules:
RewriteCond %{HTTP_HOST} ^fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
should look like this :
RewriteCond %{HTTP_HOST} ^(www\.)?fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
In order to capture bot www & non-www requests for subdomain.
Also this rule :
Redirect 301 /discover/foo /bar/foo
Will capture both requests to domain and sub-domains and using mod_rewrite here is correct not mod_alias so , replace this line with :
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^discover/foo http://example.com/bar/foo [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?(fr)\.example\.com [NC]
RewriteRule ^discover/foo http://example.com/%2/bar/foo [L,R=301]
Note: clear browser cache then test.

Redirect dirty urls to a clean url

Alright so I have done the process of cleaning up my urls and everything is going good I put the following in my htaccess.
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /l/index.php?mp3=$1&img=http://www.dailynewsounds.com/wp-content/uploads/2016/01/tinashe-5535799554a1b-3.jpg&g=R\%26amp\%3BB&p=8\%20hours\%20ago&s=+Energy&a=Tinashe+Ft.+Juicy+J+ [L]
And this works to change my urls but we have our nasty urls directing to the pages.
http://dl.example.com/l/index.php?mp3=oo8aey7qw3n9&img=http://www.example.com/wp-content/uploads/2016/01/stuey-rock-ft-sy-ari-da-kid-569e83c69cf5e.jpeg&g=R%26amp%3BB&p=10%20hours%20ago&s=+Me+And+You&a=Stuey+Rock+Ft.+Sy+Ari+Da+Kid+
How can I make it so that I can check my url to see if it contains index.php and if it does redirect it to the new page?
Try the following rules:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /l/index\.php\?\S*mp3=\S* [NC]
RewriteCond %{QUERY_STRING} \bmp3=([^\s&]+) [NC]
RewriteRule ^ /%1.html? [R=301,L]
If you're using Apache 2.4.x, then replace the L] with L,QSD].

redirect all images links from others websites to a url

My hosted images are linked in other websites , so now i want redirect those link to my site.
my images link look like this
http://example.com/uploads/images/September2014//iphone-6.gif (all image types jpg,gif,png etc)
sepetember2014/ is dynamic text that will change every month (monthname(alphabets)Year(in digits))plus extra / on its end.
now i want above url redirect to
http://example.com/media/iphone-6
i try following but didint work
RewriteCond %{HTTP_REFERER} !example.com
RewriteRule ^\.jpg $1\.html [R=301,L
my site htacess look like this
after using anubhava answer
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
RewriteCond %{HTTP_REFERER} !example\.com$
RewriteRule ^uploads/images/.+?/([^./]+)\.jpg$ /media/$1 [R=301,L,NC]
Your regex doesn't seem correct. You can use this rule in as first rule in your root .htaccess:
RewriteCond %{HTTP_REFERER} !example\.com$
RewriteRule ^uploads/images/.+?/([^./]+)\.jpg$ /media/$1.html [R=301,L,NC]

How to format a RewriteRule for a GET redirect?

in a wordpress blog I want to redirect all the new registrations to a custom registration page.
Instead of
www.example.com/wordpress/wp-login.php?action=register
I want to send them to
www.example.com/wordpress/register
So far I use
RewriteRule ^wordpress/wp-login.php?action=register$ www.example.com/wordpress/register [NC,L]
But it doesn't work :(
I also tried this rule;
RewriteCond %{QUERY_STRING} ^action=register$
RewriteRule ^wp-login[.]php$ /register [R,NC,L]
RewriteEngine On
RewriteCond %{QUERY_STRING} ^action=register$ [NC]
RewriteRule ^wordpress/wp-login.php$ http://www.example.com/wordpress/register? [L,R=301,NC]
1) This will redirect http://www.example.com/wordpress/wp-login.php?action=register EXACTLY to http://www.example.com/wordpress/register.
2) I suggest using 302 redirect code instead of 301 during testing, until you confirm it's working as intended (as modern browsers do cache 301 redirects which may produce confusing behaviour during testing, when you editing the rule often).
3) If you will see infinite redirect message issued by browser -- then you have conflicting rules.
Have this rule in $DOCUMENT_ROOT/wordpress/.htaccess file:
RewriteEngine On
RewriteBase /wordpress/
RewriteCond %{QUERY_STRING} ^action=register$ [NC]
RewriteRule ^wp-login\.php$ register? [R,NC,L]

Why isn't my .htaccess 301 redirect rule working?

I use URL rewriting on my redesigned website to give my pages tidier URLs.
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^$ index.php [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^.+/$ %{REQUEST_URI}index.php [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+\.php|(.+/)?index)$ - [R=404,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L]
This .htaccess file allows /filename to actually point to /filename.php. It all works fine.
However, I have now realised that I should set up 301 permanent redirects, so that the pages of the old website (before the redesign) can redirect to pages on the new site (for SEO and linking reasons). The pages have been reorganised, so multiple old pages will redirect to new pages, for example.
The old website did not use URL rewriting. Therefore, I want to create permanent redirects such as /about-page.php to /about, doing them manually with one rule per old page.
I have tried several things, such as...
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^about-page.php$ about [R=301,L]
...or...
Redirect 301 /about-page.php /about
...but it always ends up either not working at all (giving me a 404 error when I attempt to access /old-filename.php, or breaks everything with internal server errors. It seems to work fine if I use Redirect 301 /about-page.html /about instead, but unfortunately the old URLs used .php extensions, not .html extensions.
I believe the problem is related to one of the other rules, which redirect requests for /xyz to /xyz.php, possibly creating some endless loop. But I can't figure out how to fix it.
Any advice? Thank you very much.
Edit: Final, working .htaccess file:
DirectoryIndex index.php #
RewriteEngine On
# -- Use a permanent redirect to point the old page to the new page.
# -- The RewriteCond is needed, or a redirect loop happens in certain cases.
# -- The full URL seems to be needed, or it redirects incorrectly.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^about-page.php$ http://www.mywebsite.com/about [R=301,L]
# -- Redirect most other .php files to a 404 error, to avoid duplicate content.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+\.php|(.+/)?index)$ - [R=404,L]
# -- Redirect requests without an extension, but for a valid file, to that file.
# -- I'm not sure what the RewriteCond lines are for, but they both seem necessary.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L]
DirectoryIndex index.php # -- this sets index.php to be default file for a folder
RewriteEngine On
# -- RewriteRule ^(.+\.php|(.+/)?index)$ - [R=404,L]
# -- dude this above line redirects all php files to 404 error
# -- so delete this, its a problem not solution
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L]
RewriteRule ^about-page.php$ /about [R=301,L]
This should work, comment if problem occurs

Resources