htaccess rewrite rule for specific ajax url - ajax

I am using
RewriteCond %{HTTP_COOKIE} !^.*wordpress_logged_in.*$ [NC]
RewriteRule ^wp-admin/admin-ajax.php(.*)$ dl-file.php?file=$1 [QSA,L]
to protect worpdress files which are called with admin-ajax.php from not logged in users. But this breaks ajax for all visitors - so some things won't work for non registered users.
How could I instead just specify a rewriterule for a specific url, for instance this one:
RewriteRule ^wp-admin/admin-ajax.php?action=ajax_request&fn=download&id=(.*) [QSA,L]
id would be a number for a file.
is there a way to do this with htaccess?
I followed user hakre https://wordpress.stackexchange.com/questions/37144/how-to-protect-uploads-if-user-is-not-logged-in using dl-file.php
Thank you!
AD

You can add another RewriteCond based on %{QUERY_STRING} variable to target a specific URL:
RewriteCond %{QUERY_STRING} (?:^|&)action=ajax_request&fn=download&id= [NC]
RewriteCond %{HTTP_COOKIE} !^.*wordpress_logged_in.*$ [NC]
RewriteRule ^wp-admin/admin-ajax\.php$ dl-file.php?file=$1 [QSA,L]

Related

IBM Commerce SEO URL issue with Ajax calls

i am using WCS7 FEP7. i just enabled seo following below steps.
httpd.conf changes
RewriteRule ^/(.*)$ /webapp/wcs/stores/servlet/$1 [PT,NC]
wc-server.xml changes
<SEOConfiguration defaultUrl="" dynamicUrl="true" enable="true">
<context-root-rewrite value="/"/>
</SEOConfiguration>
I have run the keyword generation job..All SEO URLs are coming up nice.but OOB ajax calls are failing.
Add to cart ajax calling is failing..
I see http:///AjaxOrderChangeServiceItemAdd Ajax POST call is being triggered..
I am getting 404 error.
Where as Commerce server needs http:///webapp/wcs/stores/servlet/AjaxOrderChangeServiceItemAdd
I tried to put a rewrite rule to add 'webapp/wcs/stores/servlet' for this particular request..As this is a POST call (parameters in the body), it would not work properly.
RewriteRule /AjaxOrderChangeServiceItemAdd$
/webapp/wcs/stores/servlet/AjaxOrderChangeServiceItemAdd?data=$1
[NC,L]‹
Am i missing something here.?
Please help.
Try this..In the code snipped below, you need to pass your store and catalog ids respectively.
RewriteEngine on
RewriteRule ^/?$ /webapp/wcs/stores/servlet/TopCategoriesDisplay?storeId=<storeId>&catalogId=<catalogId> [L,QSA,PT]
RewriteCond %{REQUEST_URI} !^/robots.txt$
RewriteCond %{REQUEST_URI} !^/sitemap.xml(.gz)?$
RewriteCond %{REQUEST_URI} !^/solr.*$
RewriteCond %{REQUEST_URI} !^/lobtools.*$
RewriteCond %{REQUEST_URI} !^(/)?$
RewriteCond %{REQUEST_URI} !^/webapp.*$
RewriteCond %{REQUEST_URI} !^/wcsstore.*$
RewriteCond %{REQUEST_URI} !^/ConsumerDirectStorefrontAssetStore.*$
RewriteCond %{REQUEST_URI} !^/wps.*$
RewriteCond %{REQUEST_URI} !^/images/.*$
RewriteCond %{REQUEST_URI} !^/favicon\.ico
RewriteRule ^/(.*) /webapp/wcs/stores/servlet/$1?storeId=<storeId>[QSA,PT]
read this below and try it , it is a little different than what you did
http://www-01.ibm.com/support/knowledgecenter/SSZLC2_7.0.0/com.ibm.commerce.starterstores.doc/tasks/tsmshortenmadisonsurl.htm
As it's mentioned in
http://www-01.ibm.com/support/knowledgecenter/SSZLC2_7.0.0/com.ibm.commerce.starterstores.doc/tasks/tsmshortenmadisonsurl.htm
`If you are specifying a blank context root, use the following format instead:
RewriteEngine on
RewriteRule ^/(?!wcsstore)(.*) /webapp/wcs/stores/servlet/$1 [PT,L]`
The SEO engine should not care if it's an POST request. Data is sent regardless.
Be sure that you've created your AJAX call from wcf:url
http://www-01.ibm.com/support/knowledgecenter/SSZLC2_7.0.0/com.ibm.commerce.component-services.doc/refs/rwvwcfurl.htm

RewriteCond %{REQUEST_URI} not working

I am trying to redirect all requests coming in to the web server as http://portal.company.com/legacy to http://portal.company.com/wps/portal/public/legacy/legacyportlet with the following rule, but it is not working as expected.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^portal\.company\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/legacy$ [NC]
RewriteRule ^(.*)$ /wps/portal/public/legacy/legacyportlet$1 [NC,L,PT]
I have also tried
RewriteCond %{HTTP_HOST} ^portal\.company\.com$ [NC]
RewriteRule ^/legacy /wps/portal/public/legacy/legacyportlet [NC,L,PT]
Any help would be greatly appreciated!
Thanks
It doesn't look like your source or target URLs change in any way, so possibly you're better off using Apache's basic Redirect directive which just redirects one URL to another.
Use this rule:
RewriteCond %{HTTP_HOST} ^portal\.company\.com$ [NC]
RewriteRule ^legacy/?$ /wps/portal/public/legacy/legacyportlet [NC,L]
Remember that in .htaccess RewriteRule doesn't match leading slash of URI.

mod_rewrite forward shortend URL

I am looking for a way to create a short URL path for a longer URL on my page
the long url is: domain.com/tagcloud/user.html?t=1234ABCD
i would like to offer a short version of the URL to easy access it:
domain.com/t/1234ABCD
I tried a few examples but I just don't get it how I could forward these rules.
RewriteRule ^(.*)/t/$ /tagcloud/user.html?t=$1 [L]
I am also using MODX so they already use rules.
in addition my htaccess file
RewriteEngine On
RewriteBase /
# Always use www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I must keep the code snippets above in my htaccess file. The first one simply forwards http://domain.com requests to www.domain.com
The friendly URLs part is needed to translate the internal IDs of my CMS with the alias of the URL. This feature must remain because the entire site cannot be influencted by the changes I try to make in htaccess...
I simply would like to add a listener that only if the URL matches www.domain.com/t/abcd1234
Therefore I need something that identifies the www.domain.com/t/ URL
your help is much appreciated
Try this:
RewriteCond %{REQUEST_URI} ^/t/.*
RewriteRule ^t/(.*)$ /tagcloud/user.html?t=$1 [R=301,L]

How to rewrite exact url while passing other paths through

I'm new to mod_rewrite, so I know this is probably a simple fix. I want to rewrite www.abc.com to www.xyz.com ONLY. I only want to do this in firefox. I do not want www.abc.com/def to redirect to www.xyz.com. Currently, I have the following:
RewriteCond %{HTTP_HOST} ^www.abc.com$ [NC]
RewriteCond %{HTTP_USER_AGENT} "firefox" [NC]
RewriteRule ^(.*)$ https://www.xyz.com [L,R=302]
I understand that the ^(.*)$ portion is what is allowing everything else to be redirected as well. What should I enter in there that will ONLY redirect www.abc.com?
Thank you!
Change the rule to
RewriteRule ^/?$ https://www.xyz.com [L,R=302]

Mapping a secondary domain to a subdirectory using mod_rewrite

I am looking to map a secondary domain to a subfolder on my document root.
For example, if requests to the domain www.example.com map to my DocumentToot, then requests to www.exampletwo.com go to /sites/files/.
I am unable to accomplish a redirect from www.exampletwo.com/index.html to www.exampletwo.com/sites/files/index.html while making the URL still display www.exampletwo.com/index.html. Any ideas?
I believe you're looking for something like this:
RewriteCond %{HTTP_HOST} ^(www\.)?exampletwo\.com [NC]
RewriteRule ^/(.*) /sites/files/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^/(.*) http://www.exampletwo.com/$1 [L,R]
RewriteCond %{HTTP_HOST} ^(www\.)?exampletwo\.com [NC]
RewriteRule ^/(.*) http://www.exampletwo.com/sites/files/$1 [L,P]
The P flag uses the proxy module, therefore the url is not changed (no redirect) on the client.

Resources