Rewriting url conditionally and making everything lowercase - apache

So, I've urls like this: https://foobar.com/blog?category=Foo+Bar , where the actual get parameter value may or may not have plus( + ) character in it.
What I'm trying to do is first make parameter value lowercase, in all cases, and then, if parameter value contains plus character( + ), replace it with hyphen.
In the end, I'm trying to get https://foobar.com/blog?category=Foo+Bar rewritten to https://foobar.com/category/foo-bar
This is what I got so far:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/blog
RewriteCond %{QUERY_STRING} category=([^/]+)+([^/]+)$
RewriteRule (.*) /category/%1-%2? [R=301,L]
Now, that's somewhat close, but it actually rewrites https://foobar.com/blog?category=Foo+Bar to https://foobar.com/category/Foo+Ba-r.
Any help would be much appreciated.

Please add RewriteMap lc int:tolower at last of your htaccess file or in your <VirtualHost> section. Could you please try following then.
RewriteEngine On
RewriteCond https on
RewriteCond %{REQUEST_URI} ^/blog$ [NC]
RewriteCond %{QUERY_STRING} ^([^=]*)=([^+]*)\+(.*)$
RewriteRule ^(.*)$ /%1/${lc:%2}-${lc:%3} [QSD,R=301,L]
Testing with curl command(with http but rule above takes care of https part):
curl -IL "http://localhost:80/blog?category=Foo+Bar"
HTTP/1.1 301 Moved Permanently
Server: Apache/2.4.46 (Win64) OpenSSL/1.1.1g PHP/7.4.11
Location: http://localhost/category/foo-bar

Related

.htaccess - Internal Server Error

I'm trying to make the following rewrite:
http://example.com/folder1/folder3/index.html
to:
http://example.com/folder1/folder2/folder3/index.html
for that I've tried the following .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^folder1/folder2/.*$
RewriteRule ^folder1/(.*)$ /folder1/folder2/$1 [L]
But I get: Internal Server Error
In the other hand, if I do an experiment (just for testing) like:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^folder1/folder2/.*$
RewriteRule ^folder1/(.*)$ /kkk/folder2/$1 [L]
Then the rewrite:
http://example.com/folder1/folder3/index.html
to:
http://example.com/kkk/folder2/folder3/index.html
works. But I need the first rewrite.
Any idea on how to solve this?
[edited word "redirection" -> word "rewrite"]
[edited to add the error.log content]
On the following jsbin you have the error.log content:
http://jsbin.com/fajohugase/1/edit?output
The URI in REQUEST_URI variable contains the leading / as it is the path part of your URL. So, when you're using the !^folder1 condition, it fails to match, causing an infinite loop.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/folder1/folder2/
RewriteRule ^folder1/(.*)$ /folder1/folder2/$1 [L]
As to your redirection, you are not actually redirecting the client; but merely internally rewriting the URL to include /folder2 in it. For a redirect, you'll need the R flag.

Rewrite part of url with .htaccess

I need to rewrite URL and save top level domain and query.
I tried to use these rules
RewriteEngine On
RewriteBase /
RewriteRule ^domain(.*)$ http://newdomain$1 [R=302,NE,L]
Using this testing tool I found that it works if domain.com/query?param=value is used as a request URL. But if I try to use http://domain.com/query?param=value[ it doesn't work.
Basically I don't care what protocol is (http or https), I just need to replace first occurrence of domain string and rewrite it with newdomain saving all other parts of a request URL.
As it turned out in your comment to the first answer I gave you are actually trying to replace only part of the hostname of the incoming request but keep path and query string. That was not clear to me from your question, sorry.
You have to use an additional RewriteCond for this, since as said before you caanot access the hostname at all inside a RewriteRule. So I guess the following goes into the direction of what you are actually looking for:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^[^\.]+\.(.+)$
RewriteCond %{HTTP_HOST} !^newdomain\.(.+)$
RewriteRule ^(.*)$ http://newdomain.%1/$1 [R=301,L,QSA]
You may want to try this modification to preserve the original request scheme too:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^[^\.]+\.(.+)$
RewriteCond %{HTTP_HOST} !^newdomain\.(.+)$
RewriteRule ^(.*)$ %{REQUEST_SCHEME}://newdomain.%1/$1 [R=301,L,QSA]

How to use htaccess Rewrite rule for just part of a url

I am trying to create a rewrite rule in my httacess file for part of the url. I want to rewrite the url if the request contains a specific string. For example change the url only if contains /members/
So
mydomain.com/members/
mydomain.com/members/activity/ros1...
mydomain.com/members/ay/bd...
ALL above should change to another url because matches /members/ string in the url
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET /members/(.*)
RewriteRule ^(.*)$ - [F,L]
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/members(.*)
RewriteRule ^(.*)$ - [F,L]
I have tried various combinations but does not seem to work. I'm sure I'm doing something really wrong as not on expert on this. Appreciate any pointers.
RewriteCond %{REQUEST_URI} ^/members
RewriteRule ^(.*)$ http://www.google.com/$1 [R, 301]
Would be something i would try to redirect a request for /members and send them to another domain. (with the same request uri). Further info on mod_rewrite can be found https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule
You are using the F flag in your rewrites. The flag is to send the Forbidden status back to client. If you want to redirect the user to another URL, you'll need to pass that URL as the second parameter to RewriteRule directive with the R flag:
RewriteEngine On
RewriteRule ^members\b http://some-other-website.com [R]

removing directory in apache mod_rewrite

I have a PHP site which replaces an ASP site, so the path structure is different.
In the URLs, I need to match http://apache.site/Cartv3/Details.asp & redirect to another location. What is the correct syntax to match that URL fragment?
I've already tried
RewriteCond %{REQUEST_URI} CartV3/results1.asp?Category=60
RewriteRule ^(.*)$ home-study/A-Levels/1/page-1 [R=301,L]
and
RewriteRule ^CartV3/Details\.asp?ProductID=1004 home-study/A-Levels/1/page-1 [R=301,L]
You meed to read more about mod_rewrite. Remember RewriteRule doesn't match query string. You attempt needs to be rewritten as:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^Category=60$ [NC]
RewriteRule ^CartV3/results1\.asp$ /home-study/A-Levels/1/page-1? [R=302,L,NC]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
PS: ? after page-1 is a special mod_rewrite syntax to strip original query string. If you want to keep original query string in rewritten URL then take out ? in the end.
The problem here is that you are trying to match the query string, which has to be done by a separate RewriteCond. If you want the match specifically "Category=60", then you can add it as a Condition:
RewriteCond %{QUERY_STRING} Category=60
RewriteCond %{REQUEST_URI} /CartV3/results1.asp
RewriteRule .* home-study/A-Levels/1/page-1?
This will match http://example.com/CartV3/results1.asp?Category=60 and redirect. The ? at the end of the rule stops "?Category=60" being to the resulting URI.
If you don't care about the value in the query string, then you can remove the first condition.

direct all webpage requests with .index.html to /

I want to direct all requests for any URL that ends with index.html to /. I have one domain on the server.
Example:
If someone wants "www.thissite.com/index.html--it is directed to www.thissite.com/.
AND
if someone wants "www.thissite.com/anyword/index.html"--it is directed to www.thissite.com/.
AND
if someone wants "www.thissite.com/folderdoesntexistonthissite/index.html"--it is directed to www.thissite.com/.
What is the .htaccess code that would enable this? (Both the rewritecondition and rewriterule)
This doesn't quite do the job:
RewriteCond %{THE_REQUEST} index\.html [NC]
RewriteRule index\.html$ http://www.thissite.com/$1 [R=301.L]
You could try this (without RewriteCond):
RewriteRule /index\.html$ http://www.thissite.com/ [R=301,NC,L]
Maybe the Error was the Period in [R=301.L].
You will need to use %{REQUEST_URI} variable to match in RewriteCond otherwise Apache strips out starting / in RewriteRule. Use below code in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_URI} ^.*/index.html$ [NC]
RewriteRule . / [R=301,L]