I am having an issue trying to remove trailing slashes from the end of a query string in apache.
I have the following rewrite rules in place right now to make the URL and Query String all lowercase:
RewriteEngine On
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} ^[^A-Z]*[A-Z].* [OR]
RewriteCond %{QUERY_STRING} ^[^A-Z]*[A-Z].*
RewriteRule ^ ${lc:%{REQUEST_URI}}?${lc:%{QUERY_STRING}} [L,R=301]
I have tried to add:
RewriteCond %{QUERY_STRING} (.+)/$
RewriteRule ^ %1 [R=301,L]
But it breaks the website. I have been searching for a way to do this but haven't come up with any solutions yet. I tried the answers from this post but they didn't work.
The reason I need to do this is because our application firewall looks for "ID" in the url and if there is any non alphanumeric character that comes after then it blocks the request. The firewall is implemented after the Apache request hits the server.
Hoping someone with more experience with Apache Rewrite rules can help me out. Thanks in advance.
To remove trailing slash from query string you can use this rule:
RewriteCond %{QUERY_STRING} ^(.+)/$
RewriteRule ^ %{REQUEST_URI}?%1 [R=301,L,NE]
Make sure this is first rule in your .htaccess below RewriteEngine On line.
Related
I need to set 301 redirect in htaccess for URLs having parameter P. One example URL is
http://www.price4india.co.in/vivo-x20-plus-ud-price-in-india-scanner-feature-real.html?p=1028
to
http://www.price4india.co.in/vivo-x20-plus-ud-price-in-india-scanner-feature-real.html
After redirect everything after .html shall get removed and the value after P=...... can be any numerical value. So far I have tried below query but it is not working. Any suggestion please...
RewriteCond %{QUERY_STRING} ^p(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]
With your shown samples, please try following .htaccess rules file. Make sure to keep your .html file and .htaccess files in root path only.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(.*\.html)\?p=\d+\s [NC]
RewriteRule ^ /%1? [R=301,L]
NOTE: In case you have further more rules in your .htaccess rules, which includes internal rewrite of html files then you could keep these rules above those.
RewriteCond %{QUERY_STRING} ^p(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]
This is almost correct, except the regex ^p(&|$) is incorrect. This matches p&<anything> or p exactly. Whereas you need to match p=<anything> (eg. ^p=) or p=<number> (eg. ^p=\d+). This is of course assuming the p URL parameter always occurs at the start of the URL-path (as in your example).
For example:
RewriteCond %{QUERY_STRING} ^p= [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]
An email went out with the wrong link (https://www.digitalmarketer.com/digital-marketing/content-marketing-strategy//) and we need to redirect the // to (https://www.digitalmarketer.com/digital-marketing/content-marketing-strategy/) but no matter what I try, the redirect isn't working.
They also want it to be redirected to always have https:///www at the beginning and to never have index.html at the end, so already in the .htaccess file I have:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^content\-marketing\-strategy/index\.html$ /digital-marketing/content-marketing-strategy/? [L,R=301]
I've tried adding a new RewriteRule, but this won't work:
RewriteRule ^content\-marketing\-strategy//$ /digital-marketing/content-marketing-strategy/? [L,R=301]
I'm very new to Apache and redirects so any help is much appreciated! Thank you!
Edit: Of note, this is in an .htaccess file inside of the digital-marketing folder (https://www.digitalmarketer.com/digital-marketing/.htaccess) which was done so all the above rules would only apply to the digital-marketing folder.
You can use insert rule at the end of your other rules to strip multiple // into /:
RewriteCond %{THE_REQUEST} //
RewriteRule ^.*$ /digital-marketing/$0 [R=301,L,NE]
Apache automatically strips down multiple // into one inside the pattern for RewriteRule thus captured value $0 will have all // converted into /
You can write a wildcard expression to remove trailing slashes. The below will match any HTTP or HTTPS URL that trails in a forward slash, and remove all trailing forward slashes from that URL:
RewriteRule ^(.*)/+$ $1 [R=301,L]
And more using 301 redirects, see more here: Best Practice: 301 Redirect HTTP to HTTPS (Standard Domain)
Good luck!
I see nothing in the way that the rule is written that would make it not rewrite. However you have multiple rules with the L flag that might stop processing on the rewrite at an earlier point than you are looking for. From the documentation
The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed.
(https://httpd.apache.org/docs/current/rewrite/flags.html).
You can try this page out http://htaccess.mwl.be/ to test all your rules together. You might have to rewrite them a bit to work with that page, it's not aware of the level your .htaccess file is at so you will have to rewrite all your rules to trigger from the root for example: RewriteRule ^digital\-marketing/content\-marketing\-strategy//$ /digital-marketing/content-marketing-strategy/? [L,R=301]
trying to redirect a url with ending parameter to same url without that parameter on the same domain
www.mydomain.com/efg/abc?id=303
to
www.mydomain.com/efg/abc
found this answer on the matter and try to use it but it doesn't work for me:
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteCond %{REQUEST_URI} /efg/abc
RewriteRule ^efg/abc? [L,R=302]
would appreciate any help.thanks
You can use this rule:
RewriteCond %{QUERY_STRING} ^id=303$ [NC]
RewriteRule ^efg/abc/?$ %{REQUEST_URI}? [NC,L,R=302]
? at the end of target URI will strip off any previous query string.
You will basically want to drop the query string. The last line you have with the abc? is not correct because it doesn't redirect anywhere.
Here is what should do the trick (this line alone and be sure that you have enabled modrewrite in .htaccess already):
RewriteRule ^efg/abc$ /efg/abc? [L,R=301]
I tested the above on a test domain of mine and it did the trick.
I am having some difficulty redirecting links. Here is a simple example and I hope you can tell me what I am missing that is causing it to fail.
RewriteCond %{QUERY_STRING} NAV=PIXIES
RewriteRule ^/category.asp?NAV=PIXIES /category/pop-artpixies-trade- [R=301,L,NE]
Please let me know what further information I can provide.
the query string isn't included in the URI for matching in the RewriteRule directive:
RewriteCond %{QUERY_STRING} NAV=PIXIES
RewriteRule ^/category.asp$ /category/pop-artpixies-trade- [R=301,L,NE]
If this is inside an .htaccess file, you need to remove the leading slash in your regexp match:
RewriteRule ^category.asp$ /category/pop-artpixies-trade- [R=301,L,NE]
If you want to actually remove the NAV=PIXIES from the query string (because the above rule will rewrite http://domain/category.asp?NAV=PIXIES to http://domain/category/pop-artpixies-trade-?NAV=PIXIES ) then you need to add a "?" to the end of your target:
RewriteRule ^category.asp$ /category/pop-artpixies-trade-? [R=301,L,NE]
How can I make that if the QUERY_STRING matches something it would use that rule?
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/?$ index.php?uri=$1 [L,QSA]
RewriteCond %{QUERY_STRING} uri=admin
ReqriteRule ^admin\?(.*)/?$ index.php?uri=admin&q=$1 [L,QSA]
Eg. http://localhost/admin?poll/new
After the ? should be the paramater q, the the query would be uri=admin&q=poll/new
Any idea on how I could do this?
Thanks.
Well, it happens that your problem is more simple than the link I gave you as you do not want any analysis on the query string content.
If you use this single line:
RewriteRule ^admin index.php?uri=admin&q= [L,QSA]
Where QSA mean append the query string to the result. You will obtain an internal redirection to:
index.php?uri=admin&q=&poll/new
Which is not OK, this is because the way you use argument (admin?poll/new) is not the standard way. So it seems we'll need to capture the query string content and put it by hand on the rewriteRule. This should work (if you need it only for /admin url):
RewriteCond %{REQUEST_FILENAME} admin [NC]
RewriteCond %{QUERY_STRING} (.*) [NC]
RewriteRule .* index.php?uri=admin&q=%1 [L]
Where %1 is the first parenthesis match in the RewriteCond :(.*), meaning everything in the query string, the query string being anything after the question mark. So in fact this allows admin?poll/new but also admin?poll/new&foo=toto, giving index.php?uri=admin&q=poll/new&foo=bar