Rewrite Rule for Dynamic Image - .htaccess

I am trying to create an image generator with a path that looks like a normal image path. If the image is generated on the URL http://example.com/img/gen/?v1=a&v2=b&v3=c&v4=d I want it to be accessed from this URL: http://example.com/img/gen/a/b/c/d.jpg
I am trying with this .htaccess rule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/img/gen/([^/]*)/([^/]*)/([^/]*)/([^/]*)\.jpg$ /img/gen/?sn=$1&v=$2&ref=$3&t=$4 [L]
</IfModule>
I have trying placing the file on https://example.com/ and https://example.com/img/gen/ but nothing works.

You made a classical mistake which prevents your rule from getting applied. And though there still mighty be other issues you definitely have to understand that inside a dynamic configuration file (".htaccess") the request URL is examined as a relative path. So your pattern trying to match an absolute paths can never match.
Instead implement your rule in a more flexible way:
RewriteEngine On
RewriteRule ^/?img/gen/([^/]+)/([^/]+)/([^/]+)/([^/]+)\.jpg$ /img/gen/?sn=$1&v=$2&ref=$3&t=$4 [END]
Note the leading 11/? which makes the leading slash optional. That way your implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Related

Rewrite Rule in .htaccess file

The URL in my site is https://www.artmail.xyz/single.php?id=37
I want to make it https://www.artmail.xyz/single/37.html
I added the following line in my .htaccess:
RewriteRule ^([^/]*).html$ /single.php?id=$1 [L]
But nothing is happening. No error also.
Is it possible Mod rewrite is 'off' with my server? How can I check. Please help.
Thanks
Certainly it is possible that the rewriting module is not active:
it is an optional module that needs to be loaded into the server
you need to enable it for the specific location (check the documentation for the AllowOverride directive)
you need to use the well known RewriteEngine on initially
But more important is that your matching pattern actually matches what you want it to match:
RewriteEngine on
RewriteRule ^/?single/(\d+)\.html$ /single.php?id=$1 [END]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

how to ignore path of URL with .htaccess

What is tried to accomplish without success is ignoring by .htaccess any path of a domain and just transfer it to the index page, for example
the url example.com/asb , the asb will be ignored and the page that will appear will be the main index page , (while the URL still be asb if possible)
how can I do it?
Simplest and recommended way to achieve this is by using FallbackResource
FallbackResource /index.php
As per official doc:
In earlier versions of httpd, this effect typically required mod_rewrite, and the use of the -f and -d tests for file and directory existence. This now requires only one line of configuration.
Existing files, such as images, css files, and so on, will be served normally.
This probably is the easiest approach:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^ /index.php [END]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
If the above does not show any effect, then most likely one of these issues needs to get solved:
rewriting module is not loaded into the apache http server
rewriting module is not enabled
rewriting is not permitted to the location you want it to get applied
you have implemented your rewriting rule at the wrong place
your http server is unable to read the rewriting rule
you have a syntax error in your rule set
All of these need to be tackled separately. You should start by monitoring your http server's error log file. What does it say?

Htaccess rewrite/hide for query string

How can I rewrite this in .htaccess correctly?
example.com/donner-avis/?p_id=7818
to
example.com/donner-avis/7818
Thanks a lot for your help !
This should point you into the right direction:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=(\d+)$
RewriteRule ^/donner-avis/?$ /donner-avis/%1 [R=301]
RewriteRule ^/?donner-avis/(\d+)$ /donner-avis/?p_id=$1 [END]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

If Condition in RewirteRule .htaccess

I am migrating an old website and as part of the task is the migration of the old URLs to the new format.
In the old format, we have lang2 that identifies ENglish, and in the new webpbage I see that the URL has changed to "en".
An old url looks like:
page/lang2/1111/some-title.html
I can extract the variables I need (pattern="page/([^/])/([^/])/([^/]*).html" from the above URL and handle a forward in a Rewrite Rule. What seems hard to do is alter the variable holding "lang2" into "en".
Any one has an idea how to do this?
The new URL should loook like /en/title.id and a full rule would have been:
ReWriteRule ^page\/([^/]*)\/([^/]*)\/([^/]*).html$ /$1/$3.$2 [L]
Problem is the $1 should be changed to "en" if lang2 and another code if "lang1"
So there are only two options, "lang1" and "lang2"? Then simply make two separate rules:
RewriteEngine on
RewriteRule ^/?page/lang2/(\d+)/(.*)\.html$ /en/$2.$1 [END]
RewriteRule ^/?page/lang1/(\d+)/(.*)\.html$ /xx/$2.$1 [END]
Note: "xx" obviously is meant as a placeholder, you did not specify what you mean by "another code".
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

How to write '?' symbol in htaccess

I am using htaccess. I try to pass a value in url like '/?'
like "domain.com/?detail.html"
I am retrieving the value in htaccess like
RewriteRule ^?(.*).html$ index.php?key=detail
But it returns only 'detail'. Symbol '?' not accepted. I need the the value '?detail.html'
Is there any solution?
You did not understand how URLs are build. The ? has a special meaning in it. It separates URL and query string. You cannot capture or detect it in a rewriting rule since it is not part of the URL.
If that URL really is what you get as a request, then this would rewrite it as you want:
RewriteEngine in
RewriteCond %{QUERY_STRING} ^(\w+)\.html$
RewriteRule ^/?$ /index.php?key=%1 [END]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Resources