RewriteRule htaccess doesn't works for long parameter - .htaccess

I'm actually using this rule:
RewriteRule ^([a-z]+)/([A-Za-z0-9%])/?$ index.php?get1=$1&get2=$2 [NC,L]
In order to convert this hypothetical url:
.com/index.php?get1=portfolio&get2=jY1eF7FGR7k0zFYfdioRJOzzWJpkYXeTbmqUtamRV5U%3D
in this one:
.com/portfolio/jY1eF7FGR7k0zFYfdioRJOzzWJpkYXeTbmqUtamRV5U%3D
(the get2 parameter is crypted and url encoded)
The problem is that when I insert this long parameter in get2, it give me a 404 not found page when I try to visit the desidered url, but if I try to insert random words (without special character or number) it works and give me the page. Anyone that know the reason?
Thanks.

Try with below,
RewriteRule ^([a-z]+)/([A-Za-z0-9\%]+)/?$ index.php?get1=$1&get2=$2 [NC,L,B]

Related

Create a rewrite rule based on querystring

I have many redirects in the .htaccess file. Now, I need to create a rewrite rule for a URL based on it's querystring value..
http://www.mydomain.com/subdir/RentalDetails.aspx?RentalId=1072
any traffic to this url should go to...
http://www.mydomain.com/subdir/RentalDirectory.aspx
I have researched and found some good posts on the subject and came up with this..
#discontinued properties
RewriteCond %{REQUEST_URI} ^/RentalDetails\.aspx$
RewriteCond %{QUERY_STRING} ^RentalId=1072$
RewriteRule ^(.*)$ /subdir/RentalDirectory.aspx? [R=301,L]
Unfortunately, it is not working. Could someone please help me to figure out where I have gone wrong? Many thanks in advance!
I'd guess that the RewriteCond is wrong: the uri shouldn't look like
^/RentalDetails\.aspx$
[Edited -- I finally managed to get to the actual docs, and found the query string was a red herring. However, I missed the '^' at the start of the string as well, which may be causing this]
Since the ^ at the start of the expression means "the string must start here", and the $at the end means "the string ends here", your regex is too restrictive, and won't match what you need it to.
From the apache docs, the REQUEST_URI variable excludes the query string, so that's not the issue. However, you are trying to match a url of /subdir/RentalDetails.aspx with a regex that has to start with the word /RentalDetails
Try something like
# No ^ at the start, this will match RentalDetails.aspx in any location
/RentalDetails\.aspx$
Or
# This will only match in the /subdir/ directory.
^/subdir/RentalDetails\.aspx$

htaccess rewrite, url doesn't work when i add a / at the end

I have got a problem that seems to me something simple but i'm new to htaccess and can't find the solution.
i've got the following line in my htaccess (root map)
RewriteRule ^page1/([a-zA-Z0-9_-]+)/$ page1.php?name=$1
RewriteRule ^page1/([a-zA-Z0-9_-]+)$ page1.php?name=$1
When i enter the the following url it works without a problem
www.myexample.com/page1/variable
the strange thing happens when I add a / at the end. Then the page can't get the GET value out of the URL.
Thanks for your time and help!
Get rid of the ending /$ sign on the first rule
RewriteRule ^page1/([a-zA-Z0-9_-]+) page1.php?name=$1
Or you can continue to capture data
RewriteRule ^page1/([a-zA-Z0-9_-]+)/(.*)$ page1.php?name=$1
Ultimately if you want to keep capturing more data with "/" as a delimiter, I suggest doing
RewriteRule ^page1/(.*)$ page1.php?url=$1
Then use the server side script to determine what to do.

mod_rewrite: Convert query strings to hyphenated page links

How do I convert this URL to something that looks like a page:
http://www.example.com/search-results/?action=search&type%5Bequal%5D=Blog&keywords%5Bany_words%5D=visual+basic
I want to convert the above URL to:
http://www.example.com/blogs/visual-basic
When the user clicks on the converted short link 2, it should go to the long URL 1 listed on the top.
I tried this but it does not work:
RewriteRule ^blogs/([a-zA-Z0-9_-]+)\.html$ /search-results/?action=search&type%5Bequal%5D=Blog&keywords%5Bany_words%5D=$1 [NC,L]
Either you question is wrong or your RewriteRule is wrong.
You want the user to type http://www.example.com/blogs/visual-basic
and this address to be internally rewritten to:
http://www.example.com/search-results/?action=search&type%5Bequal%5D=Blog&keywords%5Bany_words%5D=visual+basic
There's no "html" ending the URL in your user address. Thus, try this, it should work:
RewriteRule ^blogs/([a-zA-Z0-9_-]+)$ /search-results/?action=search&type%5Bequal%5D=Blog&keywords%5Bany_words%5D=$1 [NC,L]

How to redirect a URL containing smartquotes via .htaccess?

Is there a way to redirect a URL containing smart quotes via .htaccess? I'm using the following rules. Only the last one seems to work:
RewriteRule ^8-%E2%80%9Crules%E2%80%9D-for-social-advertising$ /8-rules-for-social-advertising [R=301,L]
RewriteRule ^8-“rules”-for-social-advertising$ /8-rules-for-social-advertising [R=301,L]
RewriteRule ^8-%25E2%2580%259Crules%25E2%2580%259D-for-social-advertising$ /8-rules-for-social-advertising [R=301,L]
When I surf to http://blog.eloqua.com/8-“rules”-for-social-advertising/ or http://blog.eloqua.com/8-%E2%80%9Crules%E2%80%9D-for-social-advertising it doesn't get redirected.
But if I go to http://blog.eloqua.com/8-%25E2%2580%259Crules%25E2%2580%259D-for-social-advertising everything works just fine.
What am I doing wrong? Thanks so much for your help!
You are right, it's slipping passed the rules you have provided.
The reason is because the unicode characters represented by the %E2%80%9C and %E2%80%9D (aka microsoft smartquotes) have already been turned into their unicode representation within Apache. As such you need to properly match the bytestream representing those characters within apache.
In order to properly redirect urls such as this:
http://www.example.com/8-%E2%80%9Crules%E2%80%9D-for-social-advertising
You would use a rule like this:
http://www.example.com/8-\xE2\x80\x9Crules\xE2\x80\x9D-for-social-advertising

URL Beautification using .htaccess

in search of a more userfriendly URL, how do i achieve both of the following, elegantly using only .htaccess?
/de/somepage
going to /somepage?ln=de
/zh-CN/somepage#7
going to /somepage?ln=zh-CN#7
summary:
/[language]/[pagefilenameWithoutExtension][optional anchor#][a number from 0-9]
should load (without changing url)
/[pagefilenameWithoutExtension]?ln=[language][optional anchor#][a number from 0-9]
UPDATE, after provided solution:
1. exception /zh-CN/somepage should be reachable as /cn/somepage
2. php generated thumbnails now dont load anymore like:
img src="imgcpu?src=someimage.jpg&w=25&h=25&c=f&f=bw"
RewriteRule ^([a-z][a-z](-[A-Z][A-Z])?)/(.*) /$3?ln=$1 [L]
You don't need to do anything for fragments (eg: #7). They aren't sent to the server. They're handled entirely by the browser.
Update:
If you really want to treat zh-CN as a special case, you could do something like:
RewriteRule ^zh-CN/(.*) /$1?ln=zh-CN [L]
RewriteRule ^cn/(.*) /$1?ln=zh-CN [L]
RewriteRule ^([a-z][a-z])/(.*) /$2?ln=$1 [L]
I would suggest the following -
RewriteEngine on
RewriteRule ^([a-z][a-z])/([a-zA-Z]+) /$2?ln=$1
RewriteRule ^([a-z][a-z])/([a-zA-Z]+#([0-9])+) /$2?ln=$1$3
The first rule takes care of URLs like /de/somepage. The language should be of exactly two characters
length and must contain only a to z characters.
The second rule takes care of URLs like /uk/somepage#7.

Resources