After reading several questions/answer on stackOverflow, I can't find why my htaccess rule isn't working.
This is the rule :
RewriteEngine On
RewriteRule login$ login.php [NC]
In my home folder I have the file login.php but when I call http://myserverip/login I get a 404.
When I call http://myserverip/login.php the page is displayed.
When I put anything in my htaccess I receive a internal server error which confirms my htaccess file is used.
So where the problem can be ?
Related
I want to configure the .htaccess file to work the following way:
If the visitor tries to open a url with an existing folder name, redirect to main page (index.html).
I have the following rules for that:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !/index.html$ [NC]
RewriteRule ^data/existing-folder-1$ https://example.com [R=301,NC,L]
RewriteRule ^data/existing-folder-1/$ https://example.com [R=301,NC,L]
RewriteRule ^data/existing-folder-2$ https://example.com [R=301,NC,L]
RewriteRule ^data/existing-folder-2/$ https://example.com [R=301,NC,L]
</IfModule>
And it's working fine.
If the user tries to open a forbidden type of file (like .php), send to 403.
RewriteCond %{HTTP_REFERER} !^https://example.com [NC]
RewriteRule \.(php|log|htaccess)$ - [F,L,NC]
And it's also working fine.
I don't want users to see the contents of the folders, so I have this line:
Options -Indexes
I think that's all the important details to see the background.
I have a /rd folder with a simple index.php file to redirect URLs:
$url=$_GET["url"];
if (empty($url)){
//
} else {
header("Location: " . $url, TRUE, 308);
// later on with special functions
exit();
}
Such a link on my page looks like this:
https://example.com/rd/?url=https%3A%2F%2Ffoo-bar.com%2Fexample
Please note, that index.php is not written after /rd/ and before ?url. But if it's easier to solve this issue, I can modify it.
And now the problem comes here:
When I click on such a link through my website, it works perfectly.
But when I copy this redirect-link, and try to open in a new tab/window, it says 403 Forbidden. I think because #2 rule to disable access to .php files outside my domain.
Since I want to share those redirect links, I want them to be able to open when copy-paste the links.
I also don't want anyone to access any of my .php files - so it's not an option to remove rule #2.
How can I solve this issue? How can I enable to use the redirect links "alone", while keeping the rules to prevent access to files/folders?
Thank you.
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.)
Trying to ask my question again and explain it the best I can.
I am running WAMP on windows 10. Apache version 2.4.18. My project is in www folder named hoidja.ee. mod_rewrite is enabled, AllowOverrideis set to All.
Inside that folder I have .htaccess file, where I am trying to accomplish rewrite without redirect and it does not work at all. I know the .htaccess file is working, since I can rewrite index.php to /home/ for example and it is working perfectly fine.
I have a page with all the users listing. You can click on the user, and it will take you to a url:
http://localhost/Hoidja.ee/hoidja.php?user_id=94&username=John
How I would like this to show is
http://localhost/Hoidja.ee/user/John
without any redirection.
What I have tried:
#tried first with only user_id
RewriteCond %{QUERY_STRING} ^user_id=([A-Za-z0-9-]+)$
RewriteRule ^hoidja\.php$ /user/%1? [L]
This gave me 404 that /user/94 doesn't exist.
RewriteRule ^user/([0-9]+)/([A-Za-z0-9-]+)/?$ hoidja.php?user_id=$1&username=$2 [QSA]
This doesn't do anything visually. If I enter http://localhost/Hoidja.ee/user/94/John in the url manually, it goes to that page and shows the user data, but if I enter the original url it does not rewrite it. I am using <base> as well.
I have tried all the possible ways but nothing seems to work. How can I accomplish the url rewrite?
You are getting the 404 error because you are rewriting to a non-existent path /user/94 .you will need to redirect /hoidja.php?user_id= to /user/94 and then rewrite /user/94 back to the real location.
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} ^user_id=([A-Za-z0-9-]+)$
RewriteRule hoidja\.php$ /hoidja.ee/user/%1? [L,R]
RewriteRule ^(?:hoidja\.ee/)?user/(.+)$ /hoidja.ee/hoidja.php?user_id=$1 [L]
I have an .htaccess file defined at www.mydomain/documentation/.htaccess and am trying to redirect a number of broken links to a specific page.
The .htaccess file contains the Rewrite Rule
RewriteRule ^/?(documentation/PresentationCore~System\.Windows\.RoutedEventArgs).*$
/documentation/v4.x/SciChart_WPF_v4_SDK_User_Manual.html [R,L,NC]
Note: RewriteEngine ON has already been defined. Other rules are being applied
Now in .htaccess tester the rule is being evaluated and redirects correctly when the following URL is requested:
www.mydomain.com/documentation/PresentationCore~System.Windows.RoutedEventArgs~Handled.html
However, when the same domain is entered into the browser, I get a 404 not found for this page.
I can confirm I have uploaded .htaccess to the correct folder and cleared the server cache.
Is there anything else I need to do to get this to work?
You dont need to match the folder name if htaccess is already in that folder, try :
RewriteRule ^/?(PresentationCore~System\.Windows\.RoutedEventArgs).*$
/documentation/v4.x/SciChart_WPF_v4_SDK_User_Manual.html [R,L,NC]
Any help with this would really be appreciated.
I am setting up 301 redirects in a .htaccess file to redirect hundreds of old urls to the latest live pages.
My .htaccess file is currently located in the root of the domain and looks something like this:
RewriteCond %{QUERY_STRING} ^pName=product-one$ [OR]
RewriteCond %{QUERY_STRING} ^pName=product-two$ [OR]
RewriteCond %{QUERY_STRING} ^pName=completely-different-name$
RewriteRule ^catalog/product_info.php$ http://www.mydomain.com/new-product-name-p-123.html? [R=301,L]
While researching for answers on how to bulk redirect to one url using .htaccess, I read that it would wise to change the location of the .htaccess file and place it in the specific directories to which it apply's, so requests to other directories won't even these rules.
So I have placed the .htaccess file in the specific sub directory, which is catalog, but the redirect no longer works. What am I missing here? I want the rules for the urls to be redirected to be placed in the .htaccess file inside the catalog folder so all those rewrite rules wont be loaded each time the root .htaccess is loaded.
I would suggest you change your redirect 301 line to contain the path directly to you catalog folder of OSCommerce.
So instead of using
RewriteRule ^catalog/product_info.php$ http://www.mydomain.com/new-product-name-p-123.html? [R=301,L]
use e.g.:
Redirect 301 /website/catalog http://www.mydomain.com/page2.htm
or
Redirect 301 /catalog http://www.mydomain.com/page2.htm
Option 1) is if your catalog is located at http://youdDomain.com/website/catalog/ .
If it is located at http://youdDomain.com/website/aaa/catalog/ you would use:
Redirect 301 /website/aaa/catalog http://www.mydomain.com/page2.htm
I tested this in my own webserver - hope it helps.
When you put the .htaccess in a subdirectory, you must adjust the RewriteRule pattern accordingly. mod_rewrite strips the directory prefix, where the .htaccess file is located, see RewriteRule - What is matched?.
You must change the rule to
RewriteRule ^product_info.php$ ...
If you have lots of URL paths to rewrite, you might also want to look into RewriteMap.