A while ago I rebuilt a site that was on a CMS. As expected the Google search console regularly comes up with URLs that generate 404 crawling errors which I would promptly redirect to their new equivalents using a 301 on a .htaccess file.
The issue is that I came across the URL of an old page which looked like this:
http://www.example.com/example
The new page is now called http://www.example.com/example.php.
There is, however, a folder called example too so if I add a redirect like this:
Redirect 301 /example http://www.example.com/example.php
everything in that folder also gets redirected, so a URL called http://www.example.com/example/product.php would now show up as http://www.example.com/example.php/product.php.
Is there a way to redirect the old URL successfully without having to rename the folder example?
Please clear your browser cache and then put the following code at main root .htaccess file :
DirectorySlash Off
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+example [NC]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*) /$1.php [R=302,L]
The code above will stop directoryslash and make it off so no file without slash goes to be directory as default and then check if example exists in QUERY_STRING and the trick comes here in this line RewriteCond %{REQUEST_FILENAME}\.php -f,it will check if adding .php to this string , is that really file in directory or not ? and already we have example.php in root so , it will go to last line to redirect any string that passed the rules above to itself.php and that what you want exactly.
The above code will catch any example in only root directory with example only but if wanna inherit other sub-folders or apply this rule to entire site so , whenever there is string without extension the priority will be for the file , put the following code at your main root .htaccess file :
DirectorySlash Off
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*) /$1.php [R=302,L]
After testing this code , if it is Ok , change 302 to 301 to be permanent redirection
Related
So this is what I'm trying to accomplish, and I'll do my best to explain...
Through the .htaccess file, I'm attempting to redirect all pages of one site to another site, but ONLY if the URL doesn't exist. If the URL exists, then do nothing. Load that URL. If the URL doesn't exist, redirect to the homepage of another site.
So, going to www.johnny.com/boxing (no files) or even just www.johnny.com (no files) would redirect to www.johnpunches.com, since neither of those URLs work. And that would work for any URL on www.johnny.com that doesn't exist. It would simply redirect to the index page of www.johnpunches.com
However, going to www.johnny.com/members (where there is a working directory) would actually go to that URL and not redirect, because the page exists.
So, again, if the URL exists, then do nothing. Load that URL. If the URL doesn't exist, redirect to the homepage of another site.
RewriteEngine On
# File does not exist
RewriteCond %{REQUEST_FILENAME} !-f
# Directory does not exist
RewriteCond %{REQUEST_FILENAME} !-d
# Redirect to new domain if the above were missing
RewriteRule ^(.+)$ https://www.google.com/$1 [NC,QSA]
Change out google.com to whatever domain you are redirecting to.
Okay, I was able to get some assistance from another thread and put this together:
RewriteEngine on
RewriteCond %{http_host} ^johnny.com [nc]
RewriteRule ^(.*)$ http://www.johnny.com/$1 [r=301,nc,l]
RewriteRule !^folder($|/) http://www.johnkicks.com [L,R=301]
In the example above, the "folder" can be replaced with any directory that works. The second and third line force the www version of the site before the final rule has run so that any URL that DOES work, like johnny.com/folder would redirect to www.johnny.com/folder.
Two questions:
Did I do it correctly? Does the order of my rules work? Should I be doing it any differently?
Would I have to duplicate that fourth line for every working directory?
I would like to write a rule to redirect that kind or URL :
https://www.mywebsite.com/blog/wp-content/uploads/2007/06/DSC_1690.jpg
TO
https://www.mywebsite.com/wp-content/uploads/2007/06/DSC_1690.jpg
without the blog directory but ONLY for /blog/wp-content, i don't want to remove the /blog enretirely, because there is the blog here. i just want to move the wp-content at the root folder.
for all files inside /blog/wp-content to /wp-content
i tried that rule but it doesn't work : RewriteRule ^blog/wp-content/(.*)$ wp-content/$1 [L,NC,R]
Thanks for your help
Try this :
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/blog/(wp-content/)(.+)\sHTTP.*$
RewriteRule ^blog/wp-content/(.*)$ /%1$1 [L,R=301]
RewriteRule ^wp-content/(.*)$ /blog/wp-content/$1 [L,NE]
With rules above , you will be able to hide /blog/wp-content from URI but still get the same path internally.
Clear browser cache then test
hi i read all of other questions but i cant resolve my problem.
i setup my joomla website in this url: https://www.example.com/new
and now i want reditect this to (https://www.example.com) with my data for example i want reditect example.com/new/all-ads-smart/103-Hotels or other to example.com/all-ads-smart/103-Hotels
also i test this but it dont work
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^new/(.*)$ https://www.example.com/$1 [R=301,L]
this just work when i open (https://www.example.com/new) and redirect me to (https://www.example.com) and when open this (https://www.example.com/new/ads.php) i give 404 error
in end i sorry for my bad english<3
my data is:
new/all-ads-smart/103-Hotels
to
/all-ads-smart/103-Hotels
RewriteRule ^new/(.*)$ https://www.example.com/$1 [R=301,L]
This actually looks OK and should "work", providing it is in the root .htaccess file and located near the top of that file. However, if you still have a .htaccess file in the /new subdirectory then this is likely to override the directive in the parent .htaccess file - so it won't do anything.
However, it would be preferable to do this in the /new/.htaccess file instead (and keep the parent .htaccess file clean). For example, remove everything in the existing /new/.htaccess file and simply have an unconditional redirect:
RewriteEngine On
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
This will override the parent .htaccess file and when anyone visits the /new subdirectory they will be redirected. (This uses mod_rewrite, as opposed to a mod_alias Redirect to ensure that it overrides the parent .htaccess file.)
Is there a way to redirect this :
http://example.com/download.php?fid=##
to another url?
I tried to use Redirect 301 in htaccess but it is not working for me.
Basically, I have an script , where you click on a button and you download a file, that is stored in your website, in your root folder, but I want to store the file externally, for example in Dropbox. If you copy the hyperlink of the button, you get http://example.com/download.php?fid=## --- where ## is query string that identifies the file.
If I remove the ?fid=## part, it works, but then all the downloads will be redirected to the same file.
lets say i have
http://domain.com/download.php?fid=40
I've tried this
RewriteCond %{QUERY_STRING} id=40`
RewriteRule ^sub-dir/download\.php$ /https://dl.dropboxusercontent.com/s/c1x0u1m64i1i186/00431.doc?dl=0/? [L,R=301]`
and Ive tried
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/download.php$
RewriteCond %{QUERY_STRING} ^fid=40$
RewriteRule ^(.*)$ https://dl.dropboxusercontent.com/s/c7x0u1m14i1i186/00431.doc?dl=0 [R=301,L]
It seems the second block of code its working for me now
I have the following htaccess file in the root of my site to redirect a directory 'MyDirectory' to another URL (to stop google indexing both sites)
RewriteCond %{HTTP_HOST} ^myurl\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.myurl\.co\.uk$
RewriteRule ^mydirectory\/(.*)$$ "http\:\/\/www\.redirectURL\.com/" [R=301,NC]
I then have another htaccess file in the 'MyDirectory' directory above which handles the URL and query string in a user friendly way:
RewriteRule ^Answers/(.+)$ Answers.php?articleName=$1 [QSA]
However, when i have the ReWriteRule ^Answers... section in my other htaccess it stops the redirect in the root of my site from working.
Any suggestions why the ^Answers/... rewrite is overwritting my redirect to www.redirectURL.com.
Thanks
.htaccess is per directory directive and for any URI path it current directory's .htaccess is processed first. If no .htaccess is found then it starts going 1 level up the directory tree until it finds one. It processes DocumentRoot/.htaccess in the end.
In your case since you have a /MyDirectory/.htaccess therefore all the directives are read from that file only thus overriding all the rules of DocumentRoot/.htaccess.
However you can add this line after RewriteEngine On in /MyDirectory/.htaccess
RewriteOptions inherit
This will process parent .htaccess after completing current one.