I am trying to get the URL http://example.com/api/dev/v1/warehouse/ to rewrite to: http://example.com/api/v1/warehouse/
The 'dev' directory in the first URL doesn't exist. The second URL exists.
Essentially, when a visiter goes to the first URL, it should rewrite (not redirect) to the second. It should also rewrite all sub directories.
I've tried the following:
RewriteEngine on
RewriteRule ^/?api/dev/v1/(.*)$ /api/v1/$1 [L]
RewriteEngine on
RewriteRule ^/api/v1/(.*)$ /api/dev/v1/$1 [L]
RewriteEngine on
RewriteRule ^/?api/dev/v1/(.*)$ /api/v1/$1 [L]
RewriteEngine on
RewriteRule !^api/dev/v1(/|$) api/v1%{REQUEST_URI} [L]
but all of them gave 404....
To add, the .htaccess file is located at the root, with nothing else in the htaccess file.
This looks pretty straight forward, actually:
RewriteEngine on
RewriteRule ^/?api/dev/v1/(.*)$ /api/v1/$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).
For this to work the rewriting module needs to be loaded into the http server, obviously. In addition, if you decide to use a dynamic configuration file (".htaccess" style file) despite what I explained above, then you need to enable the interpretation of such file for the requested location (look for AllowOverride in the apache documentation). The file needs to be placed in the DOCUMENT_ROOT folder of the http host responding to the request and it needs to be readable by the http server process. And of course it needs to have valid syntax. If all that is given and things still do not work, then you definitely should take a look into your http server's error log file. That is where the engine will log all issues it runs into. You need to monitor that file anyway when working on implementations in a web environment, no way around that.
Related
I'm trying to setup a rewrite from /api/user{/id} to api/user.php{/id} but for some reason it doesn't work. I suppose it's an internal rewrite loop but I don't know how to fix it.
RewriteCond %{REQUEST_URI} ^\/?api\/user\/?([a-zA-Z0-9_\-\/]+)?
RewriteRule ^\/?api\/user\/?(.*)?$ api/user.php/$1 [L,QSA]
Folder structure:
api/
api/user.php
When I change the rewrite rule so that it's not user but user2, it works without a problem. However, that's not what I need so it'd be kind if you could help me finding a solution to keep the naming.
This here is working with user2:
RewriteCond %{REQUEST_URI} ^\/?api\/user2\/?([a-zA-Z0-9_\-\/]+)?
RewriteRule ^\/?api\/user2\/?(.*)?$ api/user.php/$1 [L,QSA]
This probably is what you are looking for:
RewriteEngine on
RewriteRule ^/?api/user(?:/([a-zA-Z0-9_/-]*))?$ /api/user.php/$1 [END]
It matches /api/user and /api/user/ without capturing anything. For every longer requested URL the remainder is matched against the character group you specified.
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 distributed 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 distributed 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 distributed configuration files (".htaccess"). Those distributed 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).
I am searching for a few hours on how to redirect all .php files to the file without the .php.
I know there is surely a fast answer for this but I have really trouble finding it.
How can I say that if someone loads mysite.com/mysubfolder/mysecondsub/myfile.php goes to mysite.com/mysubfolder/mysecondsub/myfile?
Like a normal rewrite except the URL PHP redirects to the one without the PHP extension.
(using .htaccess if possible and mod_rewrite)
I configured Apache2.conf to allow overwrites so that I am able to use .htacess.
It works when I use something like this post htaccess remove index.php from url
But the thing is that it works if I write mysite.com/url, but if I write mysite.com/url.php it does not redirect to mysite/url (which is what I want) I tried a few solutions but still no idea.
This probably is what you are looking for:
RewriteEngine on
RewriteRule ^/?(.+)\.php$ /$1 [R=301]
RewriteCond %{REQUEST_URI} !\.php$
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/?(.+)$ /$1.php [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 distributed 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 distributed 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 distributed configuration files (".htaccess"). Those distributed 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).
I want a rewrite rule for https://example.com/dev1.0/templates/feedback.php?uid=3 to be used as https://example.com/dev1.0/templates/feedback/uid/3
This probably is what you are looking for:
RewriteEngine on
RewriteRule ^/?dev1\.0/templates/feedback/uid/(\d+)/?$ /dev1.0/templates/feedback.php?uid=$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.
If you also want to redirect old references things get a bit more complicated:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^uid=(\d+)$
RewriteRule ^/?dev1\.0/templates/feedback\.php$ /dev1.0/templates/feedback/uid/%1 [R=301]
RewriteRule ^/?dev1\.0/templates/feedback/uid/(\d+)/?$ /dev1.0/templates/feedback.php?uid=$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...
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).
I'm trying to create a site whereby I can specify links as follows:
https://www.example.com/wildcard/parameter
I would like the above link to load the index.php file in the /wildcard directory and pass the parameter to the index file as index.php?type=parameter
However have the rule so that the subdirectory name could be anything.
I've tried numerous htaccess examples, however they either load the root index.php (homepage) file or return a requested url not found message
Essentially I would like:
https://example.com/wildcard/parameter
to behave the exact same way as
https://example.com/wildcard/index.php?type=parameter
But be flexible enough allow the subdirectory to have any name.
Can anyone help?
This should point you into the right direction:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/([^/]+)/(\w+)/?$
RewriteCond /%1 -d
RewriteCond /%1/index.php -f
RewriteRule ^ /%1/index.php?type=%2 [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).
I have to redirect an URL with /?1 in the end, like
https://www.example.com/path/to/page/?1
to
https://www.example.com/other/path/to/page/
And I just can't find the solution. Important is to remove the ?1. I don't know why there is this ?1 in the end and what exactly it is used for but I can't change it.
Here is what I tried (and didn't work):
RewriteCond %{REQUEST_URI} path/to/page/\?1 [NC]
RewriteRule ^other/path/to/page/ [NE]
or:
RewriteRule ^/path/to/page/?$ /other/path/to/page/ [R=301,L]
THe reason why your attempt fails is that the "1" is not part of the URL, but of the query string. So this will probably work for you:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^1$
RewriteRule ^/?path/to/page/?$ /path/to/page/ [END,QSD]
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).