htaccess cannot match the whole URL - apache

I am accessing the following URL:
http://example.com/welcome
Where I have the following .htaccess
RewriteEngine On
RewriteRule ^(.*)$ /index.php?package=base&page=$1 [L]
I am expecting to return
package=base
page=welcome
But instead is giving me a "500 Internal Server Error".
If I change the htaccess to
RewriteEngine On
RewriteRule ^we(.*)$ /index.php?package=base&page=$1 [L]
It returns:
package=base
page=lcome
As expected, and if I change the htaccess to
RewriteEngine On
RewriteRule ^(.*)me$ /index.php?package=base&page=$1 [L]
It returns:
package=base
page=welco
As I was expecting as well.
Now the question is... why it does not math the whole URL? What I am missing here? How can I say "take everything the user passes and put it on a variable"?
Thanks!

The rewrite engine loops, so without some sort of conditions, the regex ^(.*)$ matches index.php and so on. Try
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?package=base&page=$1 [L]

Related

.htaccess Rewrite Rule does not work correctly with index.php?path

Hi I have the following old url
www.domain.de/index.php?leistungen
and the new one is
www.domain.de/leistungen
I tried the following RewriteRuile like I did it millions of times.
RewriteRule ^index.php?leistungen /leistungen/ [L,R=301]
But in this case I got the following result:
www.domain.de/path/?leistungen=
And it it routes to the root url
What is the issue in this case?
Thanks in advance.
You may use these rules in your site root .htaccess:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?$1 [L,QSA]

Redirection is not working with mod_rewrite in htaccess

I need to redirect few URIs having query string like:
/pages/foo.bar?pageId=123456 to http://some.site/spam/egg/
/pages/foo.bar?pageId=45678 to http://another.site/spaming/egging/
I have this in my htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/pages/foo.bar$
RewriteCond %{QUERY_STRING} ^pageId=123456$
RewriteRule ^.*$ http://some.site/spam/egg/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/pages/foo.bar$
RewriteCond %{QUERY_STRING} ^pageId=45678$
RewriteRule ^.*$ http://another.site/spaming/egging/ [R=301,L]
But its not working, showing 404. What am i doing wrong?
You need to move these 2 rules i.e. before all other rules just below RewriteEngine On line as other rules might be overriding this.
(Based on your comments) Your culprit rule is this rule:
RewriteRule . index.php [L]
Which is actually rewriting every request to index.php and changing value of REQUEST_URI variable to /index.php thus causing this condition to fail:
RewriteCond %{REQUEST_URI} ^/pages/foo.bar$
From your example, you get redirected to
http://some.site/spam/egg/?pageId=123456
http://another.site/spaming/egging/?pageId=45678
You can use your browser developer tools to see the redirection (in the Network tab).
Maybe the query strings in the redirected URL lead to a 404? You can add a ? at the end of your redirection to clear the query string:
RewriteCond %{REQUEST_URI} ^/pages/foo.bar$
RewriteCond %{QUERY_STRING} ^pageId=45678$
RewriteRule ^.*$ http://another.site/spaming/egging/? [R=301,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]

Rewrite in htaccess trying to do a transparent redirect: rules with [R=301] works but not [L]

I have some rewriteRule in my .htaccess.
I'm trying to do a transparent redirect for all urls starting with _ (like: http://mydomain/_hello to http://mydomain/object/hello). Here's what I tried:
RewriteRule ^_([A-Za-z0-9_-]+)$ object/$1 [L] but it gives me a "Page not found" error.
When I change to [R=301,L] (not [R=301]), it works but the url changes in the browser.
My full .htaccess:
RewriteEngine on
RewriteBase /
RewriteRule ^_([A-Za-z0-9_-]+)$ index.php/object/view_by_alias/$1 [L]
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
I really don't understand what's happening?
Try this:
Add Options -Multiviews above the rules
Change back to [L]
Also, note that you don't need QSA since you're not adding anything to the query string.

subdomain htaccess

I have here a subdomain which i wish to pass on.
here the example of url : http://subdomain.domain.com/login
and it should point to : http://subdomain.domain.com/index.php/login
i write a simple htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index\.php|images|robots\.txt|css|javascript)
RewriteRule ^(.*)$ index.php/$1 [L]
but i always get 500 server error. any body have idea where i wrong?
thanks for any help
It's normal, you go to http://subdomain.domain.com/login, get redirected to http://subdomain.domain.com/index.php/login, then to http://subdomain.domain.com/index.php/index.php/login and so on because you RewriteRule always match.
You can write `RewriteRule ^([^/]*)$ index.php/$1 [L]
Make sure Apache rewrite module is active and your .htaccess file has the following line before any rewrite rule:
RewriteEngine On
Assuming you have mod_rewrite enabled, your RewriteRule causes an infinite redirection loop, which exceeds the maximum number of redirects and causes an internal server error.
You need to condition your rule so it only rewrites once. For example, this should work:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [L]