Restrict URL Paramater by htaccess rule - .htaccess

I'm trying to restrict parameter by htaccess below are the example which i want to achieve
This parameter is valid & htaccess rule will be allowing it
example.com/test?param=somevalue
This parameter should be restrict OR change parameter name by htaccess
Parameter Sample URL
example.com/test?param[]=somevalue
Redirect to
example.com/test?param=somevalue
OR
example.com/test
I tried some rules of htaccess and restricting parameter without these bracket
[]
but i'm not able to restrict parameters which containing [] in name.

You can catch the brackets by escaping them in the condition:
RewriteCond %{QUERY_STRING} (?:^|&)(foo)(?:\[\])?=(.*)(?:&|$)
RewriteRule ^ %{REQUEST_URI}?%1=%2
This rewrites https://example.com/test?foo[]=bar to https://example.com/test?foo=bar.
Note however that there is a sematic meaning for parameters with a trailing "[]" for php applications (and maybe others): those parameters are provided as arrays to the requested resource. And they potentially occur multiple times in a request's query string, which is why they are names in such manner. So take care that you don't break the application logic by this parameter modification.

Related

URL rewrite with HTACCESS for multiple language support

I am looking to add multiple language support to my website. Is it possible to use the .htaccess file to change something like:
example.com/dir/?lang=en to example.com/en/dir/
example.com/main/?lang=de to example.com/de/main/
example.com/main/page.php?lang=de to example.com/de/main/page.php
Where this works with any possible directories - so if for instance later on I made a new directory, I wouldn't need to add to this. In the above example, I want the latter to be what the user types in/is in the address bar, and the start to be how it is used internally.
Is it possible to use the .htaccess file to change something like:
example.com/dir/?lang=en to example.com/en/dir/
Yes, except that you don't change the URL to example.com/en/dir/ in .htaccess. You change the URL to example.com/en/dir/ in your internal links in your application, before you change anything in .htaccess. This is the canonical URL and is "what the user types in/is in the address bar" - as you say.
You then use .htaccess to internally rewrite the request from example.com/en/dir/, back into the URL your application understands, ie. example.com/dir/?lang=en (or rather example.com/dir/index.php?lang=en - see below). This is entirely hidden from the user. The user only ever sees example.com/en/dir/ - even when they look at the HTML source.
So, we need to rewrite /<lang>/<url-path>/ to /<url-path>/?lang=<lang>. Where <lang> is assumed to be a 2 character lowercase language code. If you are offering only a small selection of languages then this should be explicitly stated to avoid conflicts. We can also handle any additional query string on the original request (if this is requried). eg. /<lang>/<url-path>/?<query-string> to /<url-path>/?lang=<lang>&<query-string>.
A slight complication here is that a URL of the form /dir/?lang=en is not strictly a valid endpoint and requires further rewriting. I expect you are relying on mod_dir to issue an internal subrequest for the DirectoryIndex, eg. index.php? So, really, this should be rewritten directly to /dir/index.php?lang=en - or whatever the DirectoryIndex document is defined as.
For example, in your root .htaccess file:
RewriteEngine On
# Rewrite "/<lang>/<directory>/" to `/<directory>/index.php?lang=<lang>"
RewriteCond %{DOCUMENT_ROOT}/$2/index.php -f
RewriteRule ^([a-z]{2})/(.*?)/?$ $2/index.php?lang=$1 [L]
# Rewrite "/<lang>/<directory>/<file>" to `/<directory>/<file>?lang=<lang>"
RewriteCond %{DOCUMENT_ROOT}/$2 -f
RewriteRule ^([a-z]{2})/(.+) $2?lang=$1 [L]
If you have just two languages (as in your example), or a small subset of known languages then change the ([a-z]{2}) subpattern to use alternation and explicitly identify each language code, eg. (en|de|ab|cd).
This does assume you don't have physical directories in the document root that consist of 2 lowercase letters (or match the specific language codes).
Only URLs where the destination directory (that contains index.php) or file exists are rewritten.
This will also rewrite requests for the document root (not explicitly stated in your examples). eg. example.com/en/ (trailing slash required here) is rewritten to /index.php?lang=en.
The regex could be made slightly more efficient if requests for directories always contain a trailing slash. In the above I've assumed the trailing slash is optional, although this does potentially create a duplicate content issue unless you resolve this in some other way (eg. rel="canonical" link element). So, in the code above, both example.com/en/dir/ (trailing slash) and example.com/en/dir (no trailing slash) are both accessible and both return the same resource, ie. /dir/index.php?lang=en.

Need rewrite rule with wildcard

I need a htaccess rewrite rule to rewrite:
https://example.com/accounts/{user}/info
to
https://example.com/accounts/info
The {user} will be a dynamic part where this directory is not available so it needs to be a wildcard part within the rule.
Since it will be a URL where query posts will be sent to I need to forward all query params.
To internally rewrite the request (ie. the URL does not change in the browser's address bar), you can do something like the following in the .htaccess file in the document root:
RewriteEngine On
RewriteRule ^accounts/[^/]+/info$ accounts/info [L]
The "wildcard" regex [^/]+ matches 1 or more characters except the slash (so it doesn't eat into the next path segment). If {user} should match a specific pattern then the regex should be made more specific.
Any query string parameters that are on the original request are passed through to the target URL.

.htaccess, virtual directories, and semi-complex URLs

I'm basically just trying to have a master syntax for predictable URLs. Simple URL is no problem
RewriteEngine on
# RewriteRule ^friendlyUrl/content/?$ /index.php?app=main&module=content
Which to my understanding looks for the url structure and allows 1 or 0 trailing "/"'s
But some parts of the website have a /urlPrefix/ to access, eg. mysite.com/membersArea/
and /membersArea/ will be apart of every query there. I'm having trouble accomodating for trailing ?s and &s in URLs like these.
RewriteRule ^secureUrl/\?(.*)$ /index.php?app=admin&$1
This is my attempt to handle everything from mysite.com/secureUrl/ to mysite.com/secureUrl/?var1=foo&var2=bar and after many server errors and a search, I find myself here.
This is the most complex line I have and between you and me, I couldn't tell you exactly what's happening other than it looks for /friendlyUrl/10DIGITKEY/(possible task)/?possiblevars=foo&var2=bar
RewriteRule ^friendlyUrl/([a-zA-Z0-9]{10})/?([a-z]*)/?\??(.*)$ /index.php?app=main&module=web&id=$1&$2&$3
Htaccess has always been my weakest subject, and as a webmaster I pay the price constantly, any help would be appreciated.
Need to input the same request to the PHP file (plus ANY query with or without ? or &) whether its just /friendlyUrl/ or /friendyUrl/?var=1, /friendlyUrl/&var=1, /friendlyUrl/var=1
You're looking to keep the query string of your request URI to remain as is, or to be included in the rewritten URL after the rewrite process is done.
For this purpose, you use the QSA flag in your RewriteRule directive. So, to rewrite /friendlyUrl/10DIGITKEY/(possible task)/?possiblevars=foo&var2=bar, you'd have:
RewriteRule ^friendlyUrl/([a-z\d]{10})/([^/]*)/?$ /index.php?app=main&module=web&id=$1&task=$2 [QSA]
Notice the QSA flag at the end. Also, keep in mind that I'm passing the second match (the possible task of your URL) as another variable (named task). This variable will be empty if nothing was found.
QSA|qsappend
When the replacement URI contains a query string, the default behavior
of RewriteRule is to discard the existing query string, and replace it
with the newly generated one. Using the [QSA] flag causes the
query strings to be combined.

how to rewrite a custom url in joomla using htaccess

I am designing a News Website using joomla 2.5
I want rewrite this url:
http://domain.com/categoryname/?format=feed&type=rss
to:
http://domain.com/rss/categoryname
Note: I'm using mode_rewrite .htaccess for joomla.
please help me quickly.
thanks to every body in this site.
Apache's mod_rewrite allows you to transform a url to a different url utilizing regex patterns.
The pattern applies to the path and allows you to do your in your example write a regex pattern like /rss/(.+) which will match anything beginning with /rss/ and has at least one character after. The parenthesis are called a capturing group and you can reference that in the second parameter in the RewriteRule directive.
The second part /$1/?format=feed&type=rss, references the first captured group in the pattern and places it in the new url.
Finally you want to signify that it is the last rule to be processed with an [L] flag.
This gives you a rule of:
RewriteEngine On
RewriteRule /rss/(.+) /$1/?format=feed&type=rss [L]
If you intend to pass query strings to this new url, you will need to add an additional flag QSA which will result in [L,QSA] in place of [L].

Htaccess parameter overwrite + allow additional params?

I have a rule in my .htaccess file to overwrite ugly parameter urls with clean paths.
Here is an example:
RewriteRule ^landing(/)?([0-9]+)?(/)?$ landing/index.php?intPage=$2
So when someone goes to /landing/3, it would load the index.php page passing intPage parameters as 3.
Very simple.
However, I'm using techniques to trace Google Analytics referals, so I need to tack parameters onto this URL:
For example:
/landing/3?kt_type=ad&kt_st1=adwords&kt_st2=prize_a_us.en
The problem is that, I don't know how to retrieve the parameters that are tacked onto the URL, since the URL has already been overwritten and now I can't retrieve kt_type, kt_st1 etc.
Any idea on how to make it possible so I can still tack parameters to already overwritten URLs?
Use QSA flag. Change your rule to this:
RewriteRule ^landing/?([0-9]+)?/?$ landing/index.php?intPage=$1 [L,NC,QSA]
From official docs:
QSA - Appends any query string from the original request URL to any
query string created in the rewrite target

Resources