htaccess mod rewrite not working - apache

My server has an SSL setup on it and it has a site on there viewable in the browser via an IP.
I have an .htaccess file on the root with the following in it:
RewriteEngine on
RewriteRule ^single/([^/]*)$ single.php?url=$1 [L]
When I view the site URL:
https://100.100.100.100/single/test
I get a 404 not found. I have tried the actual URL:
https://100.100.100.100/single.php?url=test
And that works exactly as it should? Please note that isn't the actual IP
It works perfectly on a shared host. I have checked the server config and the Apache Mod rewrite is enabled.

You need the Query String Appended flag.
Try:
RewriteRule ^single/([^/]+) http://yourdomain.com/single.php?url=$1 [QSA]

Related

Redirect image filepath to external URL in Apache

I'm trying to redirect requests for an image folder to an external URL (AWS Cloudfront).
I have this in my Virtual Host config:
RewriteEngine On
RewriteRule ^/images/social_media/$ https://cdn.<domain_name>.com/images/social_media/ [R=301,NC,L]
However, it's not giving me the image name I'm requesting, and throwing a 403 Forbidden. How can I give it a wildcard to redirect all image requests to the external URL?
I have also tried:
^/images/social_media/\.(png|jpg|jpeg|gif)$ https://cdn.<domain_name>.com/images/social_media/$1 [R=301,NC,L]
With that one I get no 403. It just doesn't redirect at all.
I just want any requests to the images in that folder to redirect to the new external URL.
Edit: One thing I don't understand is why ^images worked, but not ^/images. I believe the accepted answer would show what would fix it, and explain why, and I don't have the 'why'.....yet.
I went with .htaccess and this is working:
RewriteRule ^images/social_media/(.*)$ https://cdn.<domain_name>.com/images/social_media/$1 [R=301,L,NC]

Forcing https using modrewrite in apache conf losing basic authentication credentials

I'm trying to force https with a system that uses basic authentication in the URL but when I add the following lines to the apache configuration files:
ReWriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R]
the results are 302 errors.
If someone types as the URL:
http://username:password#example.com/api_name.php
Based on the access log, I think the redirect is going to:
https:example.com/api_name.php
What I want is it to redirect to:
https://username:password#example.com/api_name.php
But I also want:
http://example.com/api_name.php
to correctly redirect to:
https://example.com/api_name.php
and not:
http://:#example.com/api_name.php
URL's without credentials contained within the string are working fine and redirect correctly.
How would I make this work?

My homepage wont go through HTTPS port

So my domain is [1] and as you can see whenever you go on that link it will get HTTP (without padlock) but if you go to any other link it will be HTTPS. Only my homepage goes through HTTP.
Important things to mention is that I use Apache.
Every my attempt to edit .htaccess file ends up by website stoping to work. My whole website is hosted on AWS and that is where I derived my SSL certificate.
I want to make that homepage load in HTTPS as well.
How should I do that?
Here is my app folder and place where I created my .htaccess file.
[1]:
Adding this to your .htaccess should do it:
RewriteEngine on
RewriteCond %{HTTPS} =off
RewriteRule ^$ https://www.urtina.com/ [L,R=301]
If it breaks Apache, check the error log. It is for the homepage only as requested.

How can I change a localhost URL with mod_rewrite or mod_proxy?

I've got MAMP running (Apache:80 MySQL:3306), and my localhost has a number of folders containing projects:
127.0.0.1/
127.0.0.1/test1/
127.0.0.1/test2/
I've got my hosts file setup properly so jlc.dev/test1 and jlc.dev/test2 are working fine.
Where I'm having trouble is I'd like to have the URL test1.dev to run the app at /test1 and the URL test2.dev to run the app at /test2. (I'm not sure what the right term is there, but I hope I've got the point across.) Based on one of the responses in this post, it sounds like it can be achieved with mod_proxy or mod_rewrite in the .htaccess file.
What I'm assuming I do is direct test1.dev to 127.0.0.1 in my hosts file, and have an .htaccess file that looks at the referring domain and rewrites to /test1 appropriately.
Little help? Thank you!
You'll need to direct both test1.dev and test2.dev to 127.0.0.1. Then add this to the htaccess file in the document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+)\.dev$ [NC]
RewriteCond $1::%1 !^([^/]*)/.*::\1
RewriteRule ^(.*)$ /%1/$1 [L]

Url rewrite not working; 404 error and no url change

I'm working on a website where I want the url
www.example.com/directory1/states/california.php
to point to
www.example.com/directory1/california
And a similar url change for the city pages as well:
www.example.com/directory1/cities/miami.php
should point to
www.example.com/directory1/miami
I'm using the following rules in my access file to change the url:
RewriteRule ^directory1/(alabama|alaska|arizona|arkansas|california|colorado|connecticut|delaware|florida|georgia|guam|hawaii|idaho|illinois|indiana|iowa|kansas|kentucky|louisiana|maine|maryland|massachusetts|michigan|minnesota|mississippi|missouri|montana|nationwide|nebraska|nevada|new_hampshire|new_jersey|new_mexico|new_york|north_carolina|north_dakota|ohio|oklahoma|oregon|pennsylvania|rhode_island|south_carolina|south_dakota|tennesee|texas|us_virgin_islands|utah|vermont|virginia|washington|west_virigina|wisconsin|wyoming)$ /directory1/states/$1.php [L]
RewriteRule ^directory1/(.*)$ /directory1/cities/$1.php [L]
However, nothing changes in the url bar and I always get a 404 not found. When I tested it with the htaccess checker, the output url is always correct. What is wrong with my rules? Is there a way to test how/if mod_rewrite is even functioning?
Some server configs require you to turn the rewrite engine on in your .htaccess file. Right at the top:
RewriteEngine on
I've also known some server configs where the file path starts/ends with a / (due to other rewrite rules already being run on the request) so perhaps allow for that to be there in the rules:
RewriteRule ^/?directory1/(alabama|alaska|arizona|arkansas|california|colorado|connecticut|delaware|florida|georgia|guam|hawaii|idaho|illinois|indiana|iowa|kansas|kentucky|louisiana|maine|maryland|massachusetts|michigan|minnesota|mississippi|missouri|montana|nationwide|nebraska|nevada|new_hampshire|new_jersey|new_mexico|new_york|north_carolina|north_dakota|ohio|oklahoma|oregon|pennsylvania|rhode_island|south_carolina|south_dakota|tennesee|texas|us_virgin_islands|utah|vermont|virginia|washington|west_virigina|wisconsin|wyoming)/?$ /directory1/states/$1.php [L]
RewriteRule ^/?directory1/(.*)/?$ /directory1/cities/$1.php [L]