apache html request parameters - apache

is there any way to disable/drop queries to html contents.
GET /alaswaq_property.html?d691f
GET /alaswaq_property.html?48fae
GET /alaswaq_property.html?8c106
GET /alaswaq_finance.html?fe082 if request contains filename.html?xxx then block it only allow filename.html < is this possible?
thanks in advance.

In your htaccess file or vhost config, add:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^[A-Za-z0-9]{5}$
RewriteRule \.html$ - [F,L]
If you want to simply include any query string at all, change the RewriteCond line to:
RewriteCond %{QUERY_STRING} !^$

Yes, you can use mod_rewrite to do this.
RewriteRule ^/alaswaq_property.html /alaswaq_property.html?
See http://wiki.apache.org/httpd/RewriteQueryString for stuff you can do for query string manipulation.

Related

Apache .htaccess rewrite parameter to directory

I've got an application that has been migrated to a newer platform. The tasks are similar and I'd like to redirect a GET parameter to a directory. For example
http://gallery/index.php?gal=ABC => http://photos/gal/ABC
and
http://gallery/?gal=DEF => http://photos/gal/DEF
and the anything that doesn't get caught redirect it to http://photos
I've tried
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^(/index.php)$ /%1/%2?
However all I get is a 404 and no redirection. Similarly, I've tried
RedirectMatch ^/index\.php\?=gal(.*) http://photos/gal/$1
but I'm having trouble escaping the ? in the original URL.
What would be the proper way of going about this?
Create a .htaccess file and insert the following code:
RewriteCond %{QUERY_STRING} ^(.+)=(.+)$
RewriteRule ^index.php$ http://photos %1/%2? [L,NC,R=301]
Your order is reversed. Rewrite it in front of you
RewriteRule /(.+)\/(.+) index.php?$1=$2
The question is old but might be still relevant for others, so I suggest a slightly different general approach:
RewriteEngine On
RewriteCond %{QUERY_STRING} key=([0-9a-zA-Z]+) [NC]
RewriteRule (.*) /%1? [R=302,L]
Notes:
QUERY_STRING in the condition checks for a param name "key" and catches it's value
The rewrite rule adds the param value as directory using %1. Note the ? removes the original query part from end result

Enable human readable URL's in .htaccess

Preface: Yes this question seems like duplicated, and I found related questions, but answers from there didnt help to me. :(
Hello, I want to add human readable URL's support for my PHP project. For now my URL quesry string looks like:
index.php?url=main/index
I would like to make it looks like:
index.php/main/index
I read following articles:
Stackoverflow
Cheatsheet
Stackoverflow
Stackoverflow
But when I do this:
var_dump($_GET['url']); // get empty array
, get empty array like no url parameter added.
My current .htaccess:
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ index.php?url=$1 [NC]
Can somebody help me please? Thanks!
URL: http://domain.com/index.php/controller/action
Rewritten URL: http://domain.com/index.php?url=controller/action
.htaccess
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index.php/(.*)$ /index.php?url=$1 [L,QSA]
Explanation:
The .* in the pattern ^index.php/(.*)$ matches everything after index.php/ on the incoming URL. The parentheses helps to capture the part as variable $1, which is then added at the end of the substitution URL /index.php?url= + $1.
[L, QSA]:
L ignore other rewrite rules, if this fits.
QSA means query string append.
You have
index.php?url=main/index
Yuo want to rewrite (and then user redirect) to this
index.php/main/index
What about trying this?
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^url=([a-z]+)/([a-z]+)$
RewriteRule ^.*$ http://mydomain.site/index.php/%1/%2 [R=302,L]
R=301 or 302 depend on your need
On this example i assumed that on the original url there are only a-z chars.
Just to clarify (due the fact generally the process is inverse) on this way the users will be redirect, this rules does not convert links on your page.
In line
RewriteRule ^(.*)$ index.php?url=$1 [NC] the (.*) matches the part of the url up to '?' where the query begins.
To use the values passed in the query you need the QSA flag. Means Query String Append.
If Your URL : www.abcd.com/index.php/abcd/...
.htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Try the below code, Only considered for index.php you can replace it as per your requirement. Let me know if you need further assistance.
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine ON
RewriteBase /
RewriteRule ^(index\.php)/([0-9a-zA-Z\/_-]{1,99}) index.php?url=$2
What worked for me is:-
<?php var_dump($_GET); ?> this is the only thing I did on url http://localhost/baba.php/abcd . and original .htaccess file
DirectoryIndex baba.php
RewriteEngine ON
RewriteBase /
RewriteRule ^(baba\.php)*/([0-9a-zA-Z\/_-]{1,99}) baba.php?url=$2
For Magento 2.4 Magento will always drop any GET parameters from the URL in a htaccess rewrite. The only way I found to make it work was to create a rewrite module. The setup is here: https://magento.stackexchange.com/a/158811/109113

Match Question Mark in rewrite rule using .htaccess

I have tried it in a different way that is also not working for me.
Match Question Mark in rewrite rule
please help me on this
Redirect to different url
http://localhost/crb/index.html?q=xxxxxxxx
To
http://localhost/crb/demo/result?q=xxxxxxxx
I tried this
Options +FollowSymlinks
RewriteEngine on
rewriterule ^crb/index.html?q=xxxxxxxx(.*)$ http://localhost/crb/demo/result?q=xxxxxxxx$1 [r=301,nc]
To rewrite based on query string use :
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^q=xxxxxxxx(.*)$
RewriteRule ^/?crb/index.html http://localhost/crb/demo/result?q=xxxxxxxx%1 [R=301,NC]
You could try the Query String Append (QSA) flag:
RewriteRule ^crb/index.html$ crb/demo/result [NC,L,QSA]
This should append the existing query string (q=xxxxxxxx) to the new url.
https://wiki.apache.org/httpd/RewriteFlags/QSA
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_qsa
If this code doesn't work, modify it a little and try again.

Rewriterule : php page to another (with parameters) cause a 500 error

I have a simple question but can't find the answer.
I have a page: http://www.mysite.com/overview.php?lang=en&TM=15 and
I want to access a new page : http://www.mysite.com/aboutus/overview.php?lang=en&TM=15
.htaccess
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^overview\.php\?lang=fr&TM=([0-9]+)&TM=([0-9]+)*$ /aboutus/overview.php?lang=$1&TM=$2 [R=301, L]
Any idea to solve my problem ?
Thank you in advance
Dominique
Try this,
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/overview\.php$
RewriteRule ^(.*)$ /aboutus/$1 [QSA,L]
[QSA] option will Rewrite url along with query parameters, so no need to match separately for lang=en&TM=15 (if you want to pass same request parameters while rewriting url)

Create an Alias using Apache's mod_rewrite

I need to create an alias for one exact page which unfortunately special characters in it. I do have an environment set up so other redirects work fine, but not this one:
RewriteRule ^/ow/email.htm?who=Kate%20Jones&direct=True&directemail=kate.jones#google.com$ http://www.google.com/ow/lalala.htm
How should I rewrite this statement so that it works?
PS. This is my first time here so do let me know if I'm not following stackoverflow policy correctly or smth ;) Thank you so much!
You need to use the RewriteCond directive for the URI query, either to examine QUERY_STRING:
RewriteCond %{QUERY_STRING} =who=Kate%20Jones&direct=True&directemail=kate.jones#google.com
RewriteRule ^/ow/email\.htm$ http://www.google.com/ow/lalala.htm
Or the request line in THE_REQUEST:
RewriteCond %{THE_REQUEST} ^GET\ /ow/email\.htm\?who=Kate%20Jones&direct=True&directemail=kate\.jones#google.com\s
RewriteRule ^/ow/email\.htm$ http://www.google.com/ow/lalala.htm