htaccess extract path only from THE_REQUEST into a variable - .htaccess

This should be rather simple, but after trying for several hours and also searching everywhere, all the related answers do not suffice.
I have this so far:
RewriteCond %{THE_REQUEST} /([a-zA-Z0-9-_/\.]+)?
RewriteRule ^(.*)$ - [E=TRGT:%1]
What the match should be doing is the following:
for URL example.com it should contain nothing, or not be defined
for URL example.com/ it should contain nothing, or not be defined
for URL example.com/some-word it should contain some-word
for URL example.com/some-word/ it should contain some-word
for URL example.com/some-word/?foo=bar it should contain some-word
for URL example.com/another_word/ it should contain another_word
for URL example.com/folder/file.ext it should contain folder/file.ext
for URL example.com/some/other.dot?bar=foo it should contain some/other.dot
for URL example.com/thing.ext/?foo=bar&bar=foo it should contain thing.ext
What I have so far seems to be working, except for when the request ends in some/ -or some/?thing=wat .. then it contains some/
I think I'm missing something really simple; any help will be appreciated, thank you.
UPDATE
I've managed to achieve these exact requirements with the following code, but, after trying many ways to do it in a 1-liner it fails horribly, so I did it in several lines:
RewriteCond %{THE_REQUEST} (?<=\s)(.*?)(?=\s)
RewriteRule ^(.*)$ - [E=HREFPATH:%1]
RewriteCond %{ENV:HREFPATH} (^.*)?\?
RewriteRule ^(.*)$ - [E=HREFPATH:%1]
RewriteCond %{ENV:HREFPATH} /(.*)
RewriteRule ^(.*)$ - [E=HREFPATH:%1]
RewriteCond %{ENV:HREFPATH} (.*)/$
RewriteRule ^(.*)$ - [E=HREFPATH:%1]
If anybody can reduce that to a s3xy 1(or 2)-liner I will choose your answer; thanks in advance.

Based on the accepted answer here the following fulfills the requirements of the question:
RewriteCond %{THE_REQUEST} \s/+([^?]*?)/*[\s?]
RewriteRule ^ - [E=HREFPATH:%1]

Related

Display particular (dynamicaly) URL without parent folder. Leave The Others Alone

I started learning rewrites today
First of all here are examples of my links
Index.php?action=pictures
Index.php?action=pictures&type=kitchen
Index.php?action=pictures&type=ceiling
Index.php?action=services
Index.php?action=services&type=shower
Index.php?action=services&type=windows
In my .htaccess file I have this
RewriteEngine On
RewriteBase /
#Do not Rewrite files or folders
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule ^(.*?)$ $1 [L]
#Ordinary
RewriteRule ^\.htaccess$ .htaccess [F]
RewriteRule ^(.*)/(.*)/?$ Index.php?action=$1&type=$2 [L]
RewriteRule ^(.*)/?$ Index.php?action=$1 [L]
Rewrites URLs to
localhost/pictures
localhost/pictures/kitchen
localhost/pictures/ceiling
localhost/services
localhost/services/shower
localhost/services/windows
//etc
I like to have ALL my links working as is with an exception of services where I don't need parent folder /services/.
Result:
localhost/pictures
localhost/pictures/kitchen
localhost/pictures/ceiling
localhost/services
localhost/shower
localhost/window
I tried to rewrite .htaccess but either I get only parentfolder to work or only subfolder.
I tried to add this but I do understand that this matches everything...
RewriteRule ^(.*)/?$ Index.php?action=services&type=$1 [L]
I can however hardcode it like this
RewriteRule ^window/?$ Index.php?action=services&type=window [L]
Would like to have something dynamical. If folder services -> show no folder yet still be able to see localhost/services!
Is it possible?
Think of it this way: How would Apache know if a particular string is an action, or a type of service?
Well, you have three options:
We hardcode the types of services. Anything that does not match a type must be an action.
We hardcode the actions. Anything that does not match an action must thus be a type of service.
Apache has no way of knowing: We feed it to a script, who might be able to do some magic to find out what this string is.
In your case hardcoding the actions seems like the best idea, at least when the actions are static.
RewriteRule ^(.*)/(.*)/?$ Index.php?action=$1&type=$2 [L]
RewriteRule ^(pictures|services)/?$ Index.php?action=$1 [L]
RewriteRule ^(.*)/?$ Index.php?action=services&type=$1 [L]

How To Combine These Specific Multiple .htaccess Rewrite Redirects

I cannot find a way to combine these 3 lines into one line, as a catch all for the url parameter "displayby".
The following 3 lines work, if the value is either price, date or menu_order but does anyone know how to write a directive that will catch all three of them, or even more? - as in any variable?
On the site different pages have example displayby=price, so as the examples work before, they need to redirect to the original URL - i.e.
www.example.com/category/kits/?displayby=price
to
www.example.com/category/kits/
Here is the code:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^displayby=price$
RewriteRule ^category/kits/$ https://www.example.com/category/kits/? [L,R=301]
RewriteCond %{QUERY_STRING} ^ displayby=date$
RewriteRule ^category/kits/$ https://www.example.com/category/kits/? [L,R=301]
RewriteCond %{QUERY_STRING} ^ displayby=menu_order$
RewriteRule ^category/kits/$ https://www.example.com/category/kits/? [L,R=301]
The above works perfectly, but how can I combine these into one directive that will catch all?
I do thank you in advance for your assistance.
Update
Better yet, if any URL that has a URL paramter of that type "displayby=*" can it be redirected to the URL it's attached to. Specifically for displayby.
Thank you
Try
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^displayby=(.*?)$
RewriteRule ^category/kits/$ https://www.example.com/category/kits/? [L,R=301]
(.*?) should regex match any value here without being too greedy

.htaccess change /es/ to .es

I am working with a domain like:
http://www.domain.com/es/blabla.html
And I want to change the /es part for .es and convert the URLS to something like:
http://www.domain.com.es/blabla.html
I've been trying lots of possibilities but I haven't reached a solution.
For example one of them has been:
RewriteRule ^es/(.*)$ http://www.domain.com.es/$1 [L]
But it has entered in an infinite loop. Which I've tried to avoid adding this lines:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
But the server doesn't allow this calls. So I still have the problem.
Check this.
RewriteEngine On
RewriteRule ^\.html$ /es/blabla.html [L]

Rewrite htaccess old oscommerce links

I am trying to rewrite all the old oscommerce links to a new website. But I am having trouble with part of the URL I need to rewrite.
The link looks like this:
http://www.domain.com/product_info.php?cPath=3_72&products_id=129&osCsid=6j3iabkldjcmgi3s1344lk1285
This rewrite works for the above link:
RewriteCond %{REQUEST_URI} ^/product_info\.php$
RewriteCond %{QUERY_STRING} ^cPath=3_72&products_id=129&osCsid=([A-Za-z0-9-_]+)$
RewriteRule ^(.*)$ http://www.domain.com/apple/air.html? [R=301,L]
But will not work for:
http://www.domain.com/product_info.php?cPath=3_72&products_id=129
My problem is that I want the rewrite to work no matter if the &osCsid=6j3iabkldjcmgi3s1344lk1285 part is included or not.
I think you can achieve this by not specifying the closing delimiter ($)
Give this a try:
RewriteCond %{REQUEST_URI} ^/product_info\.php$
RewriteCond %{QUERY_STRING} ^cPath=3_72&products_id=129
RewriteRule ^(.*)$ http://www.domain.com/apple/air.html? [R=301,L]
By not putting the $ at the end of the regex string you are basically saying: match any string that starts with ..., no matter what comes after
Hope this helps :)
This should do the job just fine:
RewriteCond %{QUERY_STRING} ^cPath=3_72&products_id=129
RewriteRule ^product_info\.php$ http://www.domain.com/apple/air.html? [R=301,L]
There is no need for separate condition RewriteCond %{REQUEST_URI} ^/product_info\.php$ -- this part can be (actually, SHOULD BE, for better performance) moved to RewriteRule.
This is enough ^cPath=3_72&products_id=129 -- it tells "When query strings STARTS with ...". No need to include optional/non-important parameters osCsid=([A-Za-z0-9-_]+).
This rule is to be placed in .htaccess file in website root folder. If placed elsewhere some small tweaking may be required.

mod_rewrite confusion

I have a few rewritten urls pointing to the original dynamic urls like so:
http://mysite.com/profile/edit/ => http://mysite.com/index.php?action=profile&sa=edit
Here's the rewriterule in the htaccess:
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?action=$1&sa=$2 [L]
^ So that works just fine.
Now I also have the following url:
http://mysite.com/search/editorials/ => http://mysite.com/index.php?action=search&category=editorials
The rewrite which follows the previous rewrite rule posted above:
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?action=$1&category=$2 [L]
^ This does not work. I checked the $_SERVER array and this is what's being redirected to the query string:
action=search&sa=editorials
I've been at this for a day so far, I've tried various tutorials but with no luck. How do I get this to work?
Any help appreciated. Thank you.
The two rewrites are conflicting with each other. You need to create some type of distinction between the two of them
RewriteRule ^account/(.)/([^/])/$ /index.php?action=$1&sa=$2 [L]
RewriteRule ^([^/])/([^/])/$ /index.php?action=$1&category=$2 [L]
or make the parameters consistent and use a single rewrite:
RewriteRule ^([^/])/([^/])/$ /index.php?action=$1&parm1=$2 [L]

Resources