url length decrease from htaccess - .htaccess

This is what I am going to do:
If user search
https://englishact.com/Quotation/Every+man+must+do+two+things+alone.html
htaccess will display (only five character)
https://englishact.com/Quotations/allquot.php?quotation=Every
I am trying it in htaccess but not working:
RewriteRule ^Quotation/([.]{5})(.*)\.html$ Quotations/allquot.php?quotation=$1 [NC,L] # Handle product requests
Will be helpful if you can give me edit in the code above.

You have an issue in your RegEx. [.] will match exactly a . instead if any character since the . is wrapped in [].
Try the below rule. It should work.
RewriteRule ^Quotation/(.{5})(.*)\.html$ Quotations/allquot.php?quotation=$1 [NC,L] # Handle product requests

To capture only the first five letters after Quotation/ and before the + sign you can try this:
RewriteCond %{REQUEST_URI} ^/?quotation/([a-z0-9.]{5}).*\.html$ [NC]
RewriteRule ^(.*)$ /quotations/allquot.php?quotation=%1 [NC,L]
it will accept the first five characters of the query string if they are characters between a-z (case insensitive because of NC flag),numbers 0-9 or a period.

Related

hi, i'd like to know what the difference between (.*) and ([a-zA-Z0-9]+) in the .htaccess file is

I enabled clean urls a little while ago on my website that used the first example, but then I stumbled upon the second one. Is the first just a wild card that allows all characters?
# Difference between this
RewriteEngine On
RewriteRule ^(.*)$ /index.php?/$1 [L]
# And this
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ /index.php/$1 [L]
Basically these are two different regular expressions.
. is a placeholder for any char.
[a-zA-Z0-9]+ is a placeholder for alphanumeric values
* means zero or more matches.
For more explanations you can search for RegEx using your favorite search engine.

.htaccess rewrite rule case sensitive

I have the following rewrite rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ /pages/acticle.$1.php [L]
Currently it is case sensitive. I need to remove this restriction. I've tried changing [L] to [NC,L] but it didn't work. What am I missing?
Your pattern doesn't contain any cased characters, so adding NC will have no effect. If your RewriteRule was for ^abcde and you wanted it to also match ABCDE then adding NC to the rule would do this.
The pattern in your example matches any character that is not a . or / one or more times, and then adds it to the article.$1.php result. By default it will take whatever is matched directly so abc=article.abc.php, ABC=article.ABC.php, and so on.
I'm guessing that all of your article file names are in lowercase and that you want to rewrite ABC and AbC to abc, resulting in consistent naming for the files - both get article.abc.php. If so, there are a few options listed on this page - use a loop in .htaccess to replace uppercase with lowercase before continuing, use a RewriteMap in your http.conf, or use mod_speling.

I want to remove last slash and junk characters after valid url if url also contains a valid slash in htaccess using rewrite rule

for example
valid URL - https://stackoverflow.com/questions/question2
if the URL contains https://stackoverflow.com/questions/question2/dfjhasfu$#.
then i want to remove last slash and junk characters after valid URL
RewriteCond %{THE_REQUEST}!-d `RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.+?)/([a-zA-Z0-9\.\w\W\s]+)$ $1 [R=301,L,NE]
If your url guaranteed consists of just two parts, or "questions" followed by 1 part, you can use the following rule:
RewriteRule ^(questions/[^/]+)/ $1 [R,L]
You can replace "questions" with [^/]+ (1 or more non-slash characters) if you want. Change [R] to [R=301] after testing everything works as expected.

Allow %0A in url RewriteRule with htaccess

I've got a rewriterule in my .htaccess which allows me to add unlimited parameters separated by /'s.
RewriteRule ^(.*)/?$ index.php?params=$1 [L,NC]
This works properly untill I send an urlencoded string to it (using cURL) with an encoded \n in it (%0A).
So server/param1/param2/param3text works, but server/param1/param2/param3text1%0Aparam3text2 doesn't.
I found one Q on Stack Overflow mentioning a similar problem:
How can I apply an htaccess rewrite rule to a URL containing a linefeed character (%0A)?
But I can't/don't know how to implement [\r\n] in my (.*).
Any help?
Ok, so first, I had to add a check to make sure that the file didn't exist (the two RewriteCond's take care of that). Then I had to create a pattern that matched any character, or a \r or a \n that was matched one or more times(+). The zero or more times operator (*) didn't return the results properly.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((.|\r|\n)+)/? index.php?params=$1 [L,NC]
Just an FYI here: A common hacking method called Whitespace filtering uses %0A
Filtering can be bypassed on the space character by using alternative
whitespace characters to the space character (%20). Most SQL engines
consider a line return (%0a in a *NIX environment, %0a%0d in a Windows
environment), tab characters, or the + character as valid whitespace:
You must utilize %{THE_REQUEST} variable to grab actual path from original Apache web server request.
Try this code:
RewriteCond %{QUERY_STRING} !^params=.+ [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+[^/]+/([^\s]+) [NC]
RewriteRule ^ index.php?params=%1 [L,QSA]
Then inside index.php check $_SERVER["QUERY_STRING"] for the full unadulterated path with %0A in it.

.htaccess with variables for user friendly urls

I got these two urls:
/portfolio/stamped_concrete
/p_details.php?id_cat=23&?id=91
I want to make the second url rewrite to:
/portfolio/stamped_concrete/23/91
stamped_concrete is a dynamic url which is why I'm at a loss of how to solve this. Also the two files (portfolio.php and p_details.php) are in the same directory if that matters.
How would I accomplish this?
EDIT:
stamped_concrete is also a variable string that I rewrote before and it works:
RewriteRule ^services/([a-z0-9_-]+)$ /services.php?url=$1 [NC,L,QSA]
so how would I call it within the RewriteRule?
would this be on the right track?
RewriteCond %{QUERY_STRING} url=([a-z0-9_-]+)
RewriteCond %{QUERY_STRING} id_cat=([0-9]+)
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule /p_details.php?.* /portfolio/$1/$2/$3
Try this:
RewriteCond %{QUERY_STRING} id_cat=([0-9]+)
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule /p_details.php?.* /portfolio/stamped_concrete/$1/$2
Might need to tweak it a bit -- not sure if the RewriteRule part is correct (sorry).
But, the important part is QUERY_STRING, see the apache docs: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
Also, from http://wiki.apache.org/httpd/RewriteQueryString
I always mix up the backreferences, so try it out, it's eiher dollar signs or percent signs (i really thought it was dollar signs...)
(QUOTE)
Making the Query String Part of the Path
Take a URL of the form http://example.com/path?var=val and transform it into http://example.com/path/var/val. Note that this particular example will work only for a single var=val pair containing only letters, numbers, and the underscore character.
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^/path /path/%1/%2?
(END QUOTE)
So you could probably just say "RewriteRule ^/p_details.php /portfolio/%1/%2/%3"

Resources