htaccess for restful apis with sub directories not working - .htaccess

I have a directory structure for my apis as:
api/
cameras/
index.php
...
.htaccess
bar/
set/
index.php
scan/
index.php
retrieve/
index.php
list/
index.php
Before I added the bar folder I had this working with my .htaccess folder
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php [L]
I then added this to make it work.
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule bar/set/(.*) bar/set/index.php [L]
RewriteRule bar/scan/(.*) bar/scan/index.php [L]
RewriteRule bar/list/(.*) bar/list/index.php [L]
RewriteRule bar/retrieve/(.*) bar/retrieve/index.php [L]
RewriteRule (.*) index.php [L]
They all work except the first one in the list set. What have I done wrong?

I think the order of your conditions might be causing an issue. Without seeing the URL's you are using I would have to assume. However RewriteCond only applies to the rule directly following it. You put the new rules between the conditions and your original rule. Change the order and try it this way and see how it works.
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
RewriteRule ^bar/set/(.*)$ bar/set/index.php [L]
RewriteRule ^bar/scan/(.*)$ bar/scan/index.php [L]
RewriteRule ^bar/list/(.*)$ bar/list/index.php [L]
RewriteRule ^bar/retrieve/(.*)$ bar/retrieve/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [L]

RewriteCond only gets applied to next RewriteRule. Try this code instead:
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%1]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(bar)/(scan|set|scan|retrieve)/(.*)$ $1/$2/index.php [L,NC]
RewriteRule . index.php [L]

Related

Htaccess rewrite / redirect issue from .png.webp back to png

Can any experts in .htaccess tell me why this isn't working? (it's the last line that is key - the .png.webp to png rewrite) Please help!
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} ^www.test.com [NC]
RewriteRule ^(.*)$ https://test.com/$1 [L,R=301]
RewriteRule ^(.*)\.webp.png$ $1.png [L,R=301]
Example issue:
https://centiqsap.com/wp-content/uploads-webpc/uploads/2020/07/Group-13-min-2.png.webp
Needs to rewrite and redirect to
https://centiqsap.com/wp-content/uploads-webpc/uploads/2020/07/Group-13-min-2.png
Have your htaccess file like this way once(I have added new rule and kept OP's old rules too in rules file). Please make sure you clear your cache before testing your URLs. I have also fixed some minor issues, like your https redirection rule should be at very first and you should exacpe .(dots) in regex too.
RewriteEngine ON
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.test\.com [NC]
RewriteRule ^(.*)$ https://test\.com/$1 [NE,L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^(.*\.png)\.webp/?$ $1 [R=301,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.webp -f
RewriteRule ^(.*)/?$ $1.webp [NC,L]

Drop the .html from URL in .htaccess

I am having a hard time trying to drop the .html for our newly updated website through .htaccess. I have tried a number of different lines of code copy and pasted from the internet, but I'm sure it's something simple that I just don't have the experience to see!
In short, I want to make this request (which is a 404) drop the .html:
http://mysite/cmt-grooving-sblade.html
To this:
http://mysite/cmt-grooving-sblade
Any help you could give will be I will be very grateful for!
So far, my .htaccess looks like this. The first section was already there, the rest is my recent attempt.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*)\.html$ $1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
You can use this to remove .html from your URLs:
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
It will leave you with your desired URL: http://mysite/cmt-grooving-sblade. Make sure you clear your cache before testing this.
Try this;
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

Url Rewrite - Add Query String Too 999

DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
RewriteRule ^tags tags.php [L,NC]
</IfModule>
i want to add new page .but write page not found. why? please help me.
RewriteRule ^tags tags.php [L,NC] // i want this www.example.com/tags open tags.php ?
You need to reorder your htaccess rules like this :
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteRule ^tags/?$ tags.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
</IfModule>
The problem is that, your rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
Rewrites any request (not a true file/dir) to index.php. since /tags is not an existent file on your directory, so a request for http://example.com/tags was being rewritten to /index.php and the last rule(s) never executed.

Mod Rewrite file.php? to string

I have old site with links like this:
http://domain.com/image.php?690
And I would like to change it into:
http://domain.com/old690
I have tried many different Rules, fe.:
RewriteRule ^image.php?(.+) old$1 [L]
EDIT: All rules looks like:
RewriteEngine On
RewriteRule ^admin/(.*) ninja-admin/$1 [L]
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
RewriteRule ^image.php\?(.+) old$1 [L]
What is correct RewriteRule, and why?
You're doing it upside down.
Put this code in your htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^old([0-9]+)$ image.php?$1 [L]
RewriteRule ^admin/(.*)$ ninja-admin/$1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
The rule for your images is the following
RewriteRule ^old([0-9]+)$ image.php?$1 [L]
It will forward every url like /old123 (where 123 is one or more digits) to image.php?123
EDIT: if you want to forbid direct access to image.php?xxx then you can do it this way
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/image\.php\?([0-9]+) [NC]
RewriteRule ^ old%1 [R=301,L]
RewriteRule ^old([0-9]+)$ image.php?$1 [L]
RewriteRule ^admin/(.*)$ ninja-admin/$1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
I use this so http://example.com/old4444 forwards to http://example.com/image.php?file=4444.
Also, the browser will keep http://example.com/old4444 in the address bar.
RewriteCond %{QUERY_STRING} !marker
RewriteCond %{QUERY_STRING} file=([0-9]+)
RewriteRule ^/?image.php$ %1? [R=301,L]
RewriteRule ^/?old([-a-zA-Z0-9_+]+)$ image.php?marker&file=$1 [L]

Mod rewrite remove trailing slash but keep on directories

So far my .htaccess is like this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
<FilesMatch "\.(htm|php|js|css|htc|png|gif|jpe?g|ico|xml|csv|txt|swf|flv|eot|woff|svg|ttf|pdf|gz)$">
RewriteEngine Off
</FilesMatch>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*\.html$ index.php [L]
I would like to have url with trailing slash redirected to the one without. But the slash for directories should be kept.
Any help would be very appreciated!
UPDATE:
after some more Google research i assembled some working solution for me. Both rewriting for removing trailing slash and redirect for www are working.
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^rudolfapotheke.de$ [NC]
RewriteRule ^(.*) http://www.%{HTTP_HOST}/$1 [L,R=301]
First, I would change your FilesMatch to a RewriteCond
RewriteCond %{REQUEST_FILENAME} !\.(?:htm|php|js|css|htc|png|gif|jpe?g|ico|xml|csv|txt|swf|flv|eot|woff|svg|ttf|pdf|gz)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
and simplify the pattern for the next RewriteRule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.html$ index.php [L]
or put the two rules together and test just for
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
and finally the rule for removing the trailing slash
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R,L]
To redirect those with trailing slash, you need to check if the path is a directory with a RewriteCond and then include a RewriteRule if the filename matchs /$
So it would look something like this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ $1 [R=permanent,L]

Resources