URL Rewrite a previously rewritten URL - htaccess - .htaccess

OK so... I have the following URL which works on my site:
http://my_domain.net/w/mRD3nKkM
The rewrite for this in the root of my site is:
RewriteRule ^([w])/(\w+)$ res/$1/response.php?id=$2 [L]
Simple stuff and works a treat. Now I want to redirect any traffic that hits that URL unencrypted to go over https, like below:
https://my_domain.net/w/mRD3nKkM
So I put another .htaccess file within the res/w/ folder conatining the following:
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{REQUEST_URI} ^/response.php$
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^(.*)$ https://my_domain.net/w/$1 [R,L]
To my miind this should work, but doesn't.
To be clear, I have the following URL rewrite working:
http://my_domain.net/w/mRD3nKkM
and I would like it to look like this:
https://my_domain.net/w/mRD3nKkM
Thanks

You should handle http => https before doing internal forward stuff. Following should work for you:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^w/.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]
RewriteRule ^(w)/(\w+)/?$ res/$1/response.php?id=$2 [L,NC]

Related

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].

Rewrite rule not working as expected?

I have a URL with a parameter which I wish to make into sef URL:
want:
http://map.tautktiv.com/street.php?address=abc
to become:
http://map.tautktiv.com/street/address/abc
or
http://map.tautktiv.com/address/abc
have tried several online tools to generate a .htaccess rule, but none of them have any effect on the URL, .htaccess file is active (tried to put some gibberish in it and got error 500)
these are the rules I tried:
1.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^address-([^-]*)$ /street.php?address=$1 [L]
RewriteRule street/address/(.*) street.php?address=$1
2.
Options +FollowSymLinks
RewriteEngine on
RewriteRule /address/(.*)\.php street.php?address=$1
3.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# add whatever other special conditions you need here
RewriteRule ^/([0-9]+)-(.*)$ /street.php?address=$1 [L]
RewriteRule /(.*)/(.*)/$ street.php?address=$1
the site is a sub-domain which files reside in a sub directory in a shared hosting GoDaddy server, have also tried to apply these rules to the .htaccess in the directory above it, same result.
tried also this per below suggestions
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^street/address/(.*)$ street.php?address=$1 [r=301,L]
RewriteRule ^address/(.*)$ street.php?address=$1 [r=301,L]
RewriteRule ^street/address/(.*)$ street.php?address=$1 [r=301,L]
same result, nothing happens.
tried to go directly to page from main domain but same result:
http://tautktiv.com/map/streets/street.php?address=abc
First rule will redirect your ugly URL to the pretty URL.
Second rule will internally redirect it back so the user will not see the ugly URL.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Internally forward /street/address/abc to /street.php?address=abc
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^street/address/(.*)/?$ /street.php?address=$1 [NC,L]
# Internally forward /address/abc to /street.php?address=abc
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^address/(.*)/?$ /street.php?address=$1 [NC,L]
If you confirm the rule to be working as expected then you can change it from 302 to 301 as you do not want to use 301 until you know the rule is working as expected.
The .htaccess should go inside the folder where street.php is located.
HTTP is US ASCII so your language would fail, it will redirect it to something like this:
/street/address/%25D7%2590%2520%25D7%2598%25D7%2591%25D7%25A8%25D7%2599%2520%25D7%2599%25D7%25A8%25D7%2595%25D7%25A9%25D7%259C%25D7%2599%25D7%259D%2520%25D7%2599%25D7%25A9%25D7%25A8%25D7%2590%25D7%259C
Your best bet here would be to change the links to use /street/address/word instead of the php file directly.
This way you would not need the first rule and you can use only the internal redirect which would work just fine with this update.
Try this one:
RewriteEngine on
RewriteRule ^street/address/(.*)$ street.php?address=$1 [r=301,L]
RewriteRule ^address/(.*)$ street.php?address=$1 [r=301,L]
In your examples you'd missed ^ and $ in the second row of RewriteRule.
And use [r=301,L] instead of [L] to tell the browser, that thzis is premanent redirecting.

301 redirecting a rewritten URL

I'm attempting to redirect some rewritten URL's but while the redirect is working, it is including the original parameter before the rewrite.
e.g. Redirect /used-cars.html http://www.odins.co.uk/our-cars.html
The rewritten URL is based upon the following structure:
http://www.domain.co.uk/pages/index.php?p=used-cars
However, what is happening is this:
http://www.domain.co.uk/our-cars.html?p=used-cars
The .htaccess file is as follows:
Options -Indexes
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^odins.co.uk [NC]
RewriteRule ^(.*)$ http://www.odins.co.uk/$1 [L,R=301]
RewriteRule ^([^/]*)\.html$ /pages/index.php?p=$1 [L]
RewriteRule ^news/([^/]*)$ /pages/news/index.php?n=$1 [L]
Redirect /used-cars.html http://www.domain.co.uk/our-cars.html
Any help would be appreciated.
Paul

htaccess rewrite if no rewrite was provided

First off I'm not an expert in htaccess stuff, but i try to accomplish the following for some SEO fixes.
When the url is loaded, without any params/rewrite it should get some data attached before it continues.
For example:
http://www.domain.com >>> http://www.domain.com/en/
I thought the rewrite was right like this, but didn't work (500)
RewriteCond %{REQUEST_URI} !^(en|nl|de|etc)$
RewriteRule ^(.*)$ /en/ [L,R=301]
added
RewriteRule ^(/?[^/]+) /index.php?rewrite=1 [L] # tell php we got a rewrite
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ $1/en/ [NC]

redirect my site from www.foo.bar to cdn.foo.bar

is it possible to have an htaccess rule that will redirect my files from
http://www.mydomain.com/page.html to http://cdn.mydomain.com/page.html but still making the link look like http://www.mydomain.com/page.html
I know masking urls isn't possible, but since they are on the same domain i was wondering if that was possible
Try these rules in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
# for http
RewriteCond %{HTTP_HOST} ^www.mydomain.com$ [NC]
RewriteCond %{SERVER_PORT} =80
RewriteRule ^(.*)$ http://cdn.mydomain.com/$1 [L,R]
# for https
RewriteCond %{HTTP_HOST} ^www.mydomain.com$ [NC]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^(.*)$ https://cdn.mydomain.com/$1 [L,R]
However one caveat that it is an external redirect hence URL in your browser will change to http://cdn.mydomain.com/foo because when you are jumping from one host to another you cannot have internal redirect hence R flag is needed.
No idea about .htaccess but you could use a curl script in PHP.

Resources