Unable to exclude URI from htaccess rewrite rule - .htaccess

I'm trying to redirect social media crawlers to another file to serve OG data, but I want to exclude rest api calls. All rest api calls use this uri: /api/v1. So one url could look like this: http://www.example.com/api/v1/work. I'm using a CMS (Craft) to serve data.
My htaccess file currently has this:
RewriteCond %{REQUEST_URI} !^/api/.*
RewriteRule ^ sharedata.php [L]
But if I go to http://www.example.com/api/v1/work I still get redirected to sharedata.php. Am I doing something wrong here?

It is working for me with these entries, on Apache 2.4:
RewriteCond %{REQUEST_URI} !^/api/.*$
RewriteRule ^.*$ /sharedata.php [L]
(supposing sharedata.php is placed in the root directory)

Related

.htaccess rewrite request from OLD rest endpoint NEW endpoint

I am trying to include in my .htaccess file a condition to rewrite requests to a REST endpoint that will no longer exist after I perform some updates to the location of the new endpoint. The existing request looks something like this:
https://www.example.com/applications/interface/?info&key=xxx&indentifier=xxx
I would like the new request to be similar structure, just in a different location, in the community folder:
https://www.example.com/community/applications/interface/?info&key=xxx&indentifier=xxx
Any help would be greatly appreciated. My existing .htaccess looks like below:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(js|css|jpeg|jpg|gif|png|ico|map)(\?|$) /404error.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Is there a reason why you can't just redirect the old request to the new one? Does the URI need to be internally rewritten?
If a redirect is good enough, you can do something like this (note that I'm using mod_rewrite and not mod_alias' Redirect directive, this is because they both end up getting applied to the same request and may conflict with your existing routing rule)
RewriteRule ^applications/interface/$ /community/applications/interface/ [L,R]
And you'd want that right under RewriteBase. This will take requests that look like /applications/interface/ and redirect them to /community/applications/interface/ with all of the query string parameters intact. The regex will only match that URI specifically, with the trailing / and as well.
If you need to do some sort of internal rewrite, I'd suggest doing this within your framework instead of trying to use mod_rewrite in the htaccess file. You've got it setup so every request is routed to index.php and it's up to that code to figure out what needs to go where.

htaccess redirect, everything is redirecting instead of specific requests

I'm trying to setup a new project structure. Due to some limitations of my cloud provider, I need to do some htaccess magic which I'm struggling with.
The project structure is as following
/docroot => Contains my Drupal installation
/docroot/frontend => Contains an Angular frontend
/docroot/pim => Is a symlink to /docroot.
What we need is that when we surf to example.com/pim that it redirects to the Drupal docroot /docroot. Since due to limitations of the cloud provider we can't put it in the /pim folder, they suggested to make a symlink.
Any other request to example.com should go to docroot/frontend.
So I've added the following code in my htaccess file
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/frontend.*$
RewriteRule !^pim($|/) http://example.com/frontend%{REQUEST_URI} [L,R=302]
Current behaviour is that all requests go to /frontend/index.php (I think the index.php comes from the .htaccess of Drupal, so the redirect is not done cleanely) While I would expect a request to example.com/test to direct to example.com/frontend/test
Also example.com/pim points to example.com/frontend/index.php instead of staying in the /pim directory
I've been able to fix it. :)
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/frontend.*$
RewriteCond %{REQUEST_URI} !^/pim.*$
RewriteRule ^ frontend%{REQUEST_URI} [L,R=302]
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/frontend.*$
RewriteRule ^ %{REQUEST_URI} [L]

How to rewrite url for certain pages in htaccess?

I am trying to show different url and redirect users to specific url with .htcaccess when they click on a blog post but to no avail.
Lets say the url is: http://localhost/mySite/article.php?article_title=test-title
then I would like to show it as http://localhost/mySite/article/test-title
This is my current htcaccess file:
#turn on url rewriting
RewriteEngine on
#remove the need for .php extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
#rewrite rule for blog
RewriteRule article/([A-Za-z0-9-]+) /mySite/article.php?article_title=$1
But for some reason it is not redirecting/showing the correct url. I am not getting any errors.
EDIT
Trying to ask my question again and explain it better. Let's say the url is
http://localhost/www.example.com/admin/editUser.php?user_id=126
and I would like to rewrite the url like this:
http://localhost/www.example.com/admin/user/126
then how can I achieve this. I tried using this website to check the modified url but it does not work. Seems like it does not work with any of the accepted answers here in stack at all.
This is my htaccess file atm. It is in the root of www.example.com
#turn on url rewriting
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^user/([0-9]+)/?$ /editUser.php?user_id=$1 [NC,L] # Handle user edit requests
Apache Module mod_rewrite is enabled. Also added an alias. Still no changes in the url. If I try something really basic like this:
# redirect to .php-less link if requested directly
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.+)\.php $1 [R=301,L]
it works fine.
Why is the users redirect not working? What am I doing wrong.
Try it like this for your rule for article url in mysite directory.
RewriteRule ^article/([A-Za-z0-9-]+)$ article.php?article_title=$1 [QSA,NC,L]
you need to mention start ^ and end $ of string.

Zend Framework 2 Mod Rewrite in htaccess is not working

I wrote application using ZF2 and at the end of the project I use some htaccess rewrite rules which let me to remove public keyword from application url. I found this rule in a internet tutorial and it works fine but i tried to add my own mappings and they don't work. Here is my .htaccess file content:
RewriteEngine On
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ /public/index [NC,L]
I tried to add rule
RewriteRule ^test-test-test-test-providers$ /providers?localisation=$2&serviceType=$4 [NC,L]
at the last line of file but, after checking, Zend is giving me error that requested URL can't be found in mapping.
Have you tried adding in the public dir:
RewriteRule ^test-test-test-test-providers$ /public/providers
Is there a reason why you remove the public via htaccess? The prefered solution is to set the main directory of your domain to the public folder, as described at https://framework.zend.com/manual/2.4/en/ref/installation.html - there is also a suggestion for a htaccess file. But this is not necessary when the current settings also work.
Zend is giving me error that requested URL can't be found in mapping
This one is important. I assume you mean that the error comes from the Zend Framework (Zend is only a company, it could be also the Zend). That means, that your request goes directly to the Zend Framework. Wouldn't it be better to use your route directly in the route configuration instead of remap it with your htaccess rules?
Could be possible, if the Zend Framework is still using the "test-test-test-test-providers" for it's routing. If so, you should use it in your routing configuration or test a Redirect instead of a RewriteRule; but this means, that the "test-test-test-test-providers" in the URL will be replaced by the "providers".

How do I rewrite the url?

Could someone tell me how to rewrite this URL. I have looked at a lot of questions on stackoverflow but they seem to be missing my answer.
RewriteEngine On
That is what I have... its a bit poor.
I need to rewrite url's if they do not point to a directory.
I need to do this...
any.domain.com/pages/some-page-slug/login
To be rewritten to the correct url of...
any.domain.com/pages/login.php?page=32
Does anyone have any ideas on how this can be achieved?
1) Rewriting product.php?id=12 to product-12.html
It is a simple redirection in which .php extension is hidden from the browser’s address bar and dynamic url (containing “?” character) is converted into a static URL.
RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1
2) Rewriting product.php?id=12 to product/ipod-nano/12.html
SEO expert always suggest to display the main keyword in the URL. In the following URL rewriting technique you can display the name of the product in URL.
RewriteEngine on
RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2
3) Redirecting non www URL to www URL
If you type yahoo.com in browser it will be redirected to www.yahoo.com. If you want to do same with your website then put the following code to .htaccess file. What is benefit of this kind of redirection?? Please check the post about SEO friendly redirect (301) redirect in php and .htaccess.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^optimaxwebsolutions\.com$
RewriteRule (.*) http://www.optimaxwebsolutions.com/$1 [R=301,L]
4) Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz
Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
5) Redirecting the domain to a new subfolder of inside public_html.
Suppose the you’ve redeveloped your site and all the new development reside inside the “new” folder of inside root folder.Then the new development of the website can be accessed like “test.com/new”. Now moving these files to the root folder can be a hectic process so you can create the following code inside the .htaccess file and place it under the root folder of the website. In result, www.test.com point out to the files inside “new” folder.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
RewriteCond %{REQUEST_URI} !^/new/
RewriteRule (.*) /new/$1
TO do this you need to write a front controller.
See here, here, here, and here.
Alternatively in Apache you can rewrite this
any.domain.com/pages/32/login
or this:
any.domain.com/32/login
or even this:
any.domain.com/some-slug/32/login
to this:
any.domain.com/pages/login.php?page=32
One way or another to do this with only apache you need to supply the page id in some fashion. Keep in mind even with format any.domain.com/some-slug/32/login the content of the slug is irrelevant and won't necessarily link to the correct page. Which I imagine is undesirable and bad for SEO.
Another alternative is using RewriteMap. But this will be tricky and require reloading apache configurations whenever a page/slug is created/edit.
I understand that pages and login are static in this case and some-page-slug is changing. And you always want to redirect to static page /pages/login.php?page=32
So this is how to do it:
1) Rewrite
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32
or 2) Redirect Pernament
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32 [R=301,L]
or 3) Redirect Temporary
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32 [R=302,L]
Here is great article about htaccess trics
http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/

Resources