URL re-writing rule to make url case insensitive - apache

I'm trying to get the following URLs be case insensitive and to go to same spot:
http://www.mywebsite.com/test
http://www.mywebsite.com/TEST
I have the following rule:
RewriteRule ^/([a-zA-Z0-9_\-]+)/?$ /?param=$1 [L]
It works for lowercase but when I have 'TEST' after website name, it doesn't apply.
I get The requested URL /TEST.php was not found on this server. Appending .php is another URL rule that comes after the rule above. So it looks like, it doesn't match the rule and goes on to next.

Try the following:
RewriteRule ^([a-zA-Z0-9_\-]+)?$ /index.php?param=$1
RewriteRule ^([a-zA-Z0-9_\-]+)/?$ /index.php?param=$1
Explicitly referring to the index file makes it clearer. The second rule should be added if you want to match trailing slashes on your 'test' param.
This works with both http://www.mywebsite.com/test and http://www.mywebsite.com/TEST

Related

How to create a redirect rule for 404 pages with many different path

Here my problem, the site I'm working on has many 404 pages, but they used to be the same pages with a different path
/mens/designers/mens/wales-bonner
/mens/designers/casual-shirts/wales-bonner
/mens/designers/coats-and-jackets/wales-bonner
etc.
THe client wants the redirect to go to the category, so
/mens/designers/mens/
/mens/designers/casual-shirts/
/mens/designers/coats-and-jackets/
I'm pretty sure, there must be a way to have regex rule to cover them all, but I can't seem to find how
Something like
RewriteRule ^/mens/designers/(.*)/wales-bonner /mens/designers/(.*)
but it doesn't work, I don't know how to group the middle part of the URL
Can anyone help ?
I see several potential problems with your rewrite rule:
You have a capturing group in the output URL rather than using $1 to use the result of the capturing group from the input URL.
^/ won't work in .htaccess, only in Apache .conf files. I' would use ^/? instead which will work in either context. That makes the starting slash optional.
You don't include an [R] flag on the rule to make it a redirect.
You don't include an [L] flag on the rule to prevent any following rules from interfering with it.
You can also add "mens/designers" to the capturing group so that you don't have to repeat it in the output. I would suggest:
RewriteRule ^/?(mens/designers/.*)/wales-bonner /$1 [R=301,L]

htaccess url redirect with get parameters ID and reduce value

I want to do an url redirect to a new domain by retrieving the ID parameter but only taking the first 4 characters. Anyone know how to do this?
For example, an original url:
http://www.original.example/see/news/actualite.php?newsId=be9e836&newsTitle="blablabla"
To :
https://www.new.example/actualites/be9e
I have tested :
RewriteCond %{QUERY_STRING} ^newsId=(.*)$ [NC]
RewriteRule ^$ https://www.new.example/actualites/%1? [NC,L,R]
RewriteCond %{QUERY_STRING} ^newsId=(.*)$ [NC]
RewriteRule ^$ https://www.new.example/actualites/%1? [NC,L,R]
There are a couple of problems with this:
The regex ^$ in the RewriteRule pattern only matches the document root. The URL in your example is /see/news/actualite.php - so this rule will never match (and the conditions are never processed).
The regex ^newsId=(.*)$ is capturing everything after newsId=, including any additional URL parameters. You only need the first 4 characters of this particular URL param.
As an aside, your existing condition is dependent on newsId being the first URL parameter. Maybe this is always the case, maybe not. But it is relatively trivial to check for this URL parameter, regardless of order.
Also, do you need a case-insensitive match? Or is it always newsId as stated in your example. Only use the NC flag if this is necessary, not as a default.
Try the following instead:
RewriteCond %{QUERY_STRING} (?:^|&)newsId=([^&]{4})
RewriteRule ^see/news/actualite\.php$ https://www.new.example/actualites/%1 [QSD,R,L]
The %1 backreference now contains just the first 4 characters of the newsId URL parameter value (ie. non & characters), as denoted by the regex ([^&]{4}).
The QSD flag (Apache 2.4) discards the original query string from teh redirect response. No need to append the substitution string with ? (an empty query string), as would have been required in earlier versions of Apache.
UPDATE:
I have an anchor link (#) which is added at the end of the link, is there a possibility of deleting it to make a clean link? Example, currently I have: https://www.new.example/news/4565/#title Ideally : https://www.new.example/news/4565
The "problem" here is that the browser manages the "fragment identifier" (fragid) (ie. the "anchor link (#)") and preserves this through the redirect. In other words, the browser re-appends the fragid to the redirect response from the server. The fragid is never sent to the server, so we cannot detect this server side prior to issuing the HTTP redirect.
The only thing we can do is to append an empty fragid (ie. a trailing #) in the hope that the browser discards the original fragment. Unfortunately, you will likely end up with a trailing # on your redirected URLs (browser dependent).
For example (simplified):
:
RewriteRule .... https://example.com/# [R=301,NE,L]
Note that you will need the NE flag here to prevent Apache from URL-encoding the # in the redirect response.
Like I say above, browsers might handle this differently.
Further reading:
URL Fragment and 302 redirects
redirect is keeping hash
How to clear fragment identifier on 302 redirect?

Apache rewrite rule to add query string to a url from a folder

I need to append all url's from a folder with a query param using Apache rewrite rule.
For this all url's starting with /abc/def/xyz/ the url should be appended with ?v=2
For example, /abc/def/xyz/folder/test.pdf should become /abc/def/xyz/folder/test.pdf?v=2
I tried with RewriteRule /abc/def/xyz(.*) /abc/def/xyz/$1?v=2 but it is not working.
I think you've got it only slightly wrong (start of the pattern), try this:
RewriteRule ^abc/def/xyz/(.*) /abc/def/xyz/$1?v=2
Tested here:
http://martinmelin.se/rewrite-rule-tester/

url rewrite and 404 error page not found

I am using url rewrite like this
RewriteRule ^help$ help.php
so if user come with domain.com/help it work
but if user call page like domain.com/help/ it send err 404 page not found
how can fix this that in 2 case it do same job
Try this rule:
RewriteRule ^help/?$ help`
Adding /? makes the trailing-slash to be optional. The pattern then starts to match help as well as help/
You can test this rule here
You need to make the trailing slash in your regular expression optional:
RewriteRule ^help/?$ help.php

URL Rewrite query

I want to understand what is meaning of these Rewrite rules
RewriteRule ^([^/]+)/([^/]+).html /title.php?file=$1&sub1=$2 [NC]
And
RewriteRule ^admin/reg/([^/]+) /admin.php?file=$1 [NC]
Kindly give me example, how it would be redirected to title.php
If you went to http://yourdomain/yomomma/house.html it would redirect to /title.php?file=yomamma&sub1=house
The second one http://yourdomain/admin/reg/candyfloss would redirect to /admin.php?file=candyfloss
NC makes it case insensitive.
Check out this page for more information on Regex (which is what's used in the first part of the RewriteRule) Regex Quickstart. Regex basically allows you to search strings. The example you posted picks up any characters except backslashes (which are used to break your parameters anyways).