.htaccess rule redirecting, but not rewriting - apache

I am writing a simple rewrite rule.
I want all trafic going to a given path to get contetns from another
I have these rules:
Options +FollowSymlinks
RewriteEngine on
#this is the rule I am implementing
RewriteCond %{REQUEST_URI} ^/bar/$
Rewriterule ^(.*)$ "/foo/bar/" [L]
#This rule is working
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([a-zA-Z]+)$ "/foo/bar/index.php?fb=$1&%1 [L]
There is one rule more after this one and it's working:
Like before it does not work. Like this, it does:
RewriteCond %{REQUEST_URI} ^/bar/$
Rewriterule ^(.*)$ "/foo/bar/" [R,L]
I want to rewrite, not redirect, but I often use the R just to test.
I am probably doing something wrong, but what?
Note: I had another rule before, for rewrite, that worked. Its not missing any modules (I think).

After some fighting I discovered the problem.
For this rule to work:
#this is the rule I am implementing
RewriteCond %{REQUEST_URI} ^/bar/$
Rewriterule ^(.*)$ "/foo/bar/" [L]
It needs to be written like:
RewriteCond %{REQUEST_URI} ^/bar/index.php$
Rewriterule ^(.*)$ "/foo/bar/" [L]
It needed the index.php. You should replace for the file or resource that you have.

Related

rewrite url to subdirectory

I'm new at url rewriting using appache.
I have a htaccess file with those rules:
RewriteCond %{HTTP_HOST} ^hostname/* [NC]
RewriteRule http://hostname/subDir/* [L,R]
The purpose of the rules is for rewriting the request from http://hostname to http://hostname/subDir.
I tested the rules using http://htaccess.mwl.be/ but cannot make the RewriteRule work. Tried different rules but non of them works.
Can someone help me with this simple rule?
It is a bit unclear what exactly do you want to achieve, but something like this should work:
RewriteEngine On
RewriteCond %{HTTP_HOST} =www.example.com [NC]
RewriteCond %{REQUEST_URI} !^/subDir [NC]
RewriteRule ^ subDir%{REQUEST_URI} [L]

Apache RewriteRule fails on question mark

We had a typo when creating URLs, so
/wasserh?hne/wasserhahn-1-2-zoll-dg11040-e+1281
should be redirected to
/wasserhaehne/wasserhahn-1-2-zoll-dg11040-e+1281
the .htaccess starts with
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.kull-design.com$1 [R,L=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php [L]
RewriteRule ^eng/deu index.php
and normally sth like this works
Redirect 301 /blog/tag/wasserhaehne-aus-messing/ https://www.kull-design.com/wasserhahn-classic/wasserhahn-13cm-40-593+631
but this fails
RewriteRule /wasserh?hne/wasserhahn-1-2-zoll-dg11040-e+1281 https://www.kull-design.com/wasserhaehne/wasserhahn-1-2-zoll-dg11040-e+1281
I tried to escape the ?, but that doesn't help. I suspect that the part after the ? is seen as query string, so I attempted
RewriteCond %{QUERY_STRING} hne/wasserhahn-1-2-zoll-kurz-dg11040m+1277
RewriteRule ^/wasserh https://www.kull-design.com/wasserhaehne/wasserhahn-1-2-zoll-kurz-dg11040m+1277 [R=301,L]
but that doesn't do the trick. There are similar questions, but they deal with real query strings.
Update:
I tested PanamaJacks solution using htaccess.madewithlove.be. It seems any url starting with wasserh is redirected to the same product. So i tried this instead
https://www.kull-design.com/wasserh?hne/wasserhahn-gebogen-dg110h76870+1295
RewriteEngine On
RewriteCond %{QUERY_STRING} ^hne/wasserhahn-gebogen-dg11010+1295
RewriteRule ^wasserh(.*)$ https://www.kull-design.com/wasserhaehne/wasserhahn-gebogen-dg11010+1295 [R=301,L]
but it doesn't match the condition. Again escaping - or + has no effect.
Update:
Note that you have to put these redirects before the RewriteRule, that sends anything to index.php or it won't work in spite of the rewrite-conditions being correct.
Actually this should work kinda. Give this rule a try and see if it works for you.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^.+$
RewriteRule ^wasserh(.*)$ https://www.kull-design.com/wasserhaehne/wasserhahn-1-2-zoll-kurz-dg11040m+1277? [R=301,L]
Edit:
Then just try matching part of it that is unique to that URL.
RewriteCond %{QUERY_STRING} ^hne(.+)1281$
RewriteRule ^wasserh(.*)$ https://www.kull-design.com/wasserhaehne/wasserhahn-1-2-zoll-kurz-dg11040m+1277? [R=301,L]

.htaccess is concatenating rules

I have a .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z-]+)\/([0-9]*)\/?$ ler.php?categoria=$1&id=$2
this is working for http://www.domain.com/frases/13571/
The point is that I need to redirect all HTTP to HTTPS after this rule is already applied.
So I write this:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z-]+)\/([0-9]*)\/?$ ler.php?categoria=$1&id=$2
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
But isn't working like I supposed to. I'm getting this when I perform a HTTP request:
https://www.domain.com/frases/13571/?categoria=frases&id=13571
What is the right code that just change the protocols without modify the others parts of url?
I think that my code is doing 2 requests in the first rules and that is generating this wrong url.
L flag must be applied to your rewrite rule.
You can use this code instead
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^([^/]+)/([^/]+)/$ /ler.php?categoria=$1&id=$2 [L]
Note:
you don't need to escape slashes (\/ can be /)
you'll need to clear your browser's cache before trying again with this code. Your old rule is now in cache and you won't see the new code working if you don't clear it.
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You rewrite from the original REQUEST_URI, the current is what you have captured, so try:
RewriteRule (.*) https://%{HTTP_HOST}$1 [R=301,L]
or
RewriteRule (.*) https://$1 [R=301,L]
If not working, rewrite log could be useful.

Check URL and stop processing using htaccess

I want to check URL using htaccess. Developer might want run special file - specialfile.php. I use htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /specialfile\.php$
RewriteRule .* [L] #don't change adress
RewriteRule ^$ public/index.html [NC,L]
RewriteRule (.*) public/$1 [NC,L]
My idea was: if rewriteCond %{REQUEST_URI} !/specialfile.php$ true than htaccess should use RewriteRule .* [L] - that should mean that specialfile.php will be run and this all. But it doesn't work because it runs next rule: RewriteRule (.*) public/$1 [NC,L].
I think you are using the RewriteCond not correctly. The conditions only affect the next RewriteRule that follows.
Check out the example on the Apache Homepage. Since your 2nd RewriteRule is evalutated, I think your conditions are not correct. To get a litte bit more information about the rewriting, you should increase the log level. This is also documented here.
Your 2nd rule ^$ matches only an empty request btw. That's why it probably does not work as you expect it to.

mod redirect and rewrites ( Part 2 )

Further to my previous questions i am getting my self into a mess, so i will try and lay my problem/s out as best as i can here.
I am tidying up my URL structure but have the problem that my urls are well indexed in Search engines, so i need to rewrite my urls while also redirect them.
With help earlier on a previous question i currently have the following in my .htaccess
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]
# Rewrite all index.php to root: / ( with perm redirect )
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.mydomain.com/$1 [R=301]
# Rewrite A newly added section rewrite ( no redirect required )
RewriteRule ^products/digital_imaging/([^/]*)/([^/]*)$ /products/digital_xray.php?id=$1&product=$2 [L]
# Rewrite dental news article to neat nice url
RewriteRule ^dental_news/([^/]*)/([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&redirect [L]
#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule (.*) /dental_news/%1/%2? [L,R=301]
So, this is all working as expected, but i have another url that ends in a index.php that sometimes but not always has php variables, this i also need to rewrite and redirect, by trying to do it myself i am creating a conflict of some sorts, and its not working as expected.
The URL i need to rewrite & redirect with a 301 is below
http://www.mydomain.com/news/dentistry_dental/index.php?month=February&year=2011
With the previous index.php rule it currently displays as
http://www.mydomain.com/news/dentistry_dental/?month=February&year=2011
Half way there i guess... I need it to be the following
http://www.mydomain.com/dental_news/February/2011
Baring in mind that the url sometimes has no variables as below
http://www.mydomain.com/news/dentistry_dental/index.php
Which is currently with the index.php rule above is showing as
http://www.mydomain.com/news/dentistry_dental/
This needs to be
http://www.mydomain.com/dental_news/
So yes, i am totally crap at this stuff, it is entirely new to me, so if i can get this sorted ill be a very happy bunny indeed!
Thanks All
EDIT
Ok my current .htaccess looks as below.
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]
# Rewrite all index.php to root: / ( with perm redirect )
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.mydomain.com/$1 [R=301]
RewriteRule ^products/digital_imaging/([^/]*)/([^/]*)$ /products/digital_xray.php?id=$1&product=$2 [L]
# Rewrite dental news article to neat nice url
RewriteRule ^dental_news/([^/]*)/([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&redirect [L]
#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule (.*) /dental_news/%1/%2? [L,R=301]
RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)
RewriteRule (.*) /dental_news/%1/%2? [R=301]
RewriteRule news/dentistry_dental/$ /dental_news/ [R=301]
RewriteRule ^dental_news/$ /news/dentistry_dental/index.php [L]
As it stands with this, The following occurs
www.mydomain.com/news/dentistry_dental/index.php
-> www.mydomain.com/dental_news/
( ^ Correct )
www.mydomain.com/news/dentistry_dental/index.php?month=May&year=2011
-> www.mydomain.com/dental_news/April/2011
( ^ Wrong : Showing right path in url bar, but displaying article_detail.php instead of index.php with variables)
I am guessing that 2 of the rules are similar and one is picking up the index.php with year and month variables and sending to article_detail page. I might be wrong however and i have no idea how to fix.
EDIT #2
Current .htaccess as below.
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^dentalsupportuk.com [nc]
rewriterule ^(.*)$ http://www.dentalsupportuk.com/$1 [r=301,nc]
# Rewrite all index.php to root: / ( with perm redirect )
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.dentalsupportuk.com/$1 [R=301]
RewriteRule ^products/digital_imaging/([^/]*)/([^/]*)$ /products/digital_xray.php?id=$1&product=$2 [L]
# Rewrite dental news article to neat nice url
RewriteRule ^dental_news/([0-9]*)/([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&redirect [L]
#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule (.*) /dental_news/%1/%2? [L,R=301]
RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)
RewriteRule (.*) /dental_news/%1/%2? [R=301]
RewriteRule news/dentistry_dental/$ /dental_news/ [R=301]
RewriteRule ^dental_news/([a-zA-Z]*)/([0-9]*)$ news/dentistry_dental/index.php?month=$1&year=$2&redirect [L]
www.mydomain.com/dental_news/ is now rewriting to www.mydomain.com/dental_news/ which doesnt exist so 404 error ( should be rewriting to www.mydomain.com/news/dentistry_dental/ )
www.mydomain.com/dental_news/100/some-title is now rewriting as expected to www.mydomain.com/news/dentistry_dental/article_detail.php with the variables ( working )
www.mydomain.com/news/dentistry_dental/article_detail.php?article=100&title=some-title is redirecting as expected to www.mydomain.com/dental_news/100/some-title and displaying correctly ( working )
www.mydomain.com/news/dentistry_dental/index.php?month=May&year=2010 is redirecting but crashing out Firefox stating ( Firefox has detected that the server is redirecting the request for this address in a way that will never complete. ) ( Not Working, is this some sort of loop causing this? )
www.mydomain.com/dental_news/July/2010/ the rewriting is giving me a 404 error.
So yes, im all over the place, maybe ive put in wrong order or something, but the more i mess the more im scared of breaking everything. Any ideas?
Regards
M
Try adding the follow two rules:
RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)
RewriteRule (.*) /dental_news/%1/%2? [R=301]
RewriteRule news/dentistry_dental/$ /dental_news/ [R=301]
I don't believe it causes any conflicts, and seems to cover the two scenarios you described.
EDIT
OK, so if I'm getting this correct, the URLs that are /dental_news/2968/Redefining-oral-hygiene-intervention should be rewritten to /news/dentistry_dental/article_detail.php with the proper querystrings. And then the /dental_news/April/2011 should be rewritten to /news/dentistry_dental/index.php. So...if that is the case, you should be able to add the following rule:
RewriteRule ^dental_news/([a-zA-Z]*)/([0-9]*)$ news/dentistry_dental/index.php?month=$1&year=$2&redirect [L]
It would also probably make sense to make the following update:
Current: RewriteRule ^dental_news/([^/]*)/([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&redirect [L]
Updated: RewriteRule ^dental_news/([0-9]*)/([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&redirect [L]
The difference there being that it is more specific, and the first regex group will only match a group of digits, and will help to avoid conflict with your rule that is rewriting to the index.php file.
EDIT 2
I believe I tested all of the URLs you provided, and they seem to be working now. There were a couple of redirect loops that we weren't accounting for. Hopefully this will help...
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]
# Rewrite all index.php to root: / ( with perm redirect )
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.mydomain.com/$1 [R=301]
RewriteRule ^products/digital_imaging/([^/]*)/([^/]*)$ /products/digital_xray.php?id=$1&product=$2 [L]
RewriteRule dental_news/$ /news/dentistry_dental/?rewrite [L]
# Rewrite dental news article to neat nice url
# Protect from looping because of previous rules
RewriteCond %{QUERY_STRING} !rewrite
RewriteRule ^dental_news/([0-9]*)/([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&rewrite [L]
#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule (.*) /dental_news/%1/%2? [L,R=301]
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)$
RewriteRule (.*) /dental_news/%1/%2? [R=301]
# Protect from looping because of previous rules
RewriteCond %{QUERY_STRING} !rewrite
RewriteRule news/dentistry_dental/$ /dental_news/ [R=301]
# Protect from looping because of previous rules
RewriteCond %{QUERY_STRING} !rewrite
RewriteRule ^dental_news/([a-zA-Z]*)/([0-9]*)/?$ news/dentistry_dental/index.php?month=$1&year=$2&rewrite [L]
Hope this helps.