redirect url that matches directory otherwise - .htaccess

I use redirections quite a lot but I'm not able to achieve my goal despite searching.
What I like to achieve:
a request to /aaaa redirected to /articles/aaaa.php
but only show /aaaa in browser (this already works for years)
a request to /bbbb which is a directory, no file exist with this name, has to be redirected to /products/bbbb/index.php (but only show /bbbb in browser)*
This is what I use now:
RewriteRule ^([a-z0-9-_]+)$ /articles/$1.php [NC,L]
*as alternative if it is too complex this is ok: a request to /bbbb (which is a directory no file exist with this name) redirect to /products/bbbb (and in the directory /products/bbbb I'll pick it up there with a local .htaccess and redirect it to index.php) still in the browser /bbbb should be shown.

When rewriting to the file in the /articles directory you can check that it exists first (as a file). For example:
RewriteCond %{DOCUMENT_ROOT}/articles/$1.php -f
RewriteRule ^([a-z0-9-_]+)$ /articles/$1.php [L]
I've also removed the NC flag, unless you explicitly need this?
Likewise to internally rewrite to the directory:
RewriteCond %{DOCUMENT_ROOT}/products/$1/index.php -f
RewriteRule ^([a-z0-9-_]+)$ /products/$1/index.php [L]
This actually checks that the index.php file exists, rather than the parent directory, since you are ultimately rewriting to a file, not a directory.

Related

Rewrite rule for name.123456.js in htaccess

I have this file in my webserver:
http://example.com/static/js/min/common.min.js
Since all files inside /static/ are cached with CloudFlare's Edge Cache, I need a way to change the url with something like this, so if the file is modified, the new version will be automatically fresh served:
http://example.com/static/js/min/common.min.1234567890.js
Where 1234567890 is the timestamp of the file's date modification. I already generate the filename according to the modification date, the issue I'm having is in the .htaccess file.
This works fine:
RewriteRule ^(.*)\.[\d]{10}\.(js)$ $1.$2 [L]
That means that:
http://example.com/static/js/min/common.min.1234567890.js
Is redirected to:
http://example.com/static/js/min/common.min.js
But, that will catch all .js requests from the domain, I just want to catch .js requests from within /static/js/min/*.js -- but this is failing:
RewriteRule ^static/js/min/(.*)\.[\d]{10}\.(js)$ $1.$2 [L]
What should the rule be like?
From your question,
You want to redirect
http://example.com/static/js/min/common.min.1234567890.js
to
http://example.com/static/js/min/common.min.js
So, How to do that
the .htaccess
First add
RewriteEngine On
To turn on the rewrite engine
Then the next important line comes
RewriteRule ^static/js/min/([^\d]*)(\d{10}).js$ static/js/min/$1js [L]
Explanation
Set the path as static/js/min/
Then we use RegEx to take the string until a non digit. ([^\d]*).
That is common.min. is captured.
Now $1 is common.min.
Then to match the url, we use (\d{10}) to capture the digits.
That is 1234567890 is captured.
Now $2 is 1234567890 which we don't want anymore
Then redirect to
static/js/min/$1js
Not that here we didn't added the . after the $1 because $1 ending with a . (common.min.)
So, the code will be
RewriteEngine On
RewriteRule ^static/js/min/([^\d]*)(\d{10}).js$ static/js/min/$1js [L]
Working Screenshot
My File Structure
The Address in Browser

Is it possible to wildcard a filename string to password protect a file with htaccess?

Example: wp_file*.log -- what that should do is to password protect every file whose filename start with wp_file and ends with .log -- for example wp_file-22.log and wp_file_randomfile.log.
Possible?
The way I would make this, is by adding a rewrite rule for those files, redirect to some PHP file with the origional request in the GET. The PHP file can than show a password box and eventually the content of the log file once logged in.
RewriteEngine On
RewriteRule wp_file(.*)\.log /somePHPfile.php?logFile=wp_file$1 [L]
(not tested)
If you dont need access to the log files via your website (but use e.g. ftp), than you can rewrite the requests to those files to another page
RewriteEngine On
RewriteRule wp_file(.*)\.log /index.php [L]

htaccess redirect parameter into directory

I setup a slightly weird hierarchy where index reads the URI to decide the action instead of using actual subdirectories, example: site.com/base/action/
Im moving over from the old system to this new one, but I cant get the redirects to work. What I need is: /base/?param[]=value to do an external redirect to /base/param/value/
Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.+?)(\[\])?=([^&]+)&?(.*)$
RewriteRule ^/?base/?$ /base/%1/%3/?%4 [L,R=301]
This will also preserve any query string that may be after param[]=value. You can alternatively just clobber the entire query string altogether by removing the %4 at the end of the rule's target.

Using .htaccess to redirect a certain file type from root to a sub folder

I'm trying to redirect a certain file type (PDF) from root to a sub folder, but I'm not sure how to limit the rule so that only requests for PDF files in the root are redirected while any requests for files in sub folders are left untouched.
Example:
A request for www.mydomain.com/myfile.pdf should be rewritten to www.mydomain.com/pdfs/myfile.pdf,
but a request for www.mydomain.com/other_folder/otherfile.pdf should not be redirected.
The file name can vary, but it should only apply to PDF files.
I've gotten this far, which seems to work for the first part but also means that PDFs in sub folders are being rewritten...
RewriteRule ^(.*)\.pdf$ pdfs/$1.pdf [NC]
RewriteRule ^([^/]+)\.pdf$ pdfs/$1.pdf [NC,L]
PS: this is internal redirect, if you want external redirect, use:
RewriteRule ^([^/]+)\.pdf$ pdfs/$1.pdf [NC,L,R=301]

.htaccess issue with redirecting folder

I have a folder named /test in my application.
Right now i am trying to write an .htaccess file that would show all requests to /test* as /test.
For example:
www.example.com/test/ is the actual directory with index.php file in it.
All the requests like the following should go to the same /test directory
www.example.com/test-hello/
www.example.com/test-world/
www.example.com/test-htacess/
www.example.com/test123/
Basically any requests to /test* should go to /test.
This is what I've tried so far:
RewriteRule ^/test* /test
You need to use RewriteCond to first match "test in url"
Try below:
RewriteCond %{THE_REQUEST} ^GET\ /test/
RewriteRule ^test/(.*) /test/$1 [L,R=301]
Your regular expression is wrong. You mean ^/test.*$. Your rule would match to /testtttt.
The asterisk means that the char in front of it can be zero or more times included. The dot is a special char which means here could be anything. the .* matches every string including an empty string. See also Wikipedia.
You currently are not putting the -hello, -world etc behind your folder. What is hello? Is that the file? Or the param?
The second part of the rewriteRule should be a file. Something like
RewriteRule ^/test(.*)$ /test/$1.php
Above function will have:
/testABC to /test/ABC.php
But I don't understand what you want to accomplish?

Resources