I have several files without extension for example:
NAME 1
NAME 2
They are not extension, bit I would like to insert extension .PDF using htaccess.
When accessing the url
localhost / NAME1
even without extension through htaccess recognize .pdf extension
I understand the question such that your actual files in the server side file system do have file name extensions ("/path/to/xxx.pdf"), but that this extension is missing in the requested URLs. If that assumption is corrrect (your question is vage...), then this probably is what you are looking for:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.pdf -f
RewriteRule ^/?(.+)$ /$1.pdf [END]
Here it is important that you do not simply copy and paste that snippet, but that you actually understand what is coded there, so that you can adapt it to your specific needs. The excellent documentation of the rewriting module is a good help in that: https://httpd.apache.org/docs/current/mod/mod_rewrite.html
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).
Related
I need to use .htaccess to load index file from specific directory when user vitis an url that does not exists.
Here is my folder structure:
root/realdirectory/virtualdirectory/virtualproducturl
my index file is located in root/realdirectory
And something else which makes the task more complecated :
root/realdirectory/index.php has to be loaded if user visits:
-root/realdirectory/virtualdirectory/virtualproducturl or
-root/realdirectory/virtualdirectory/
The 'virtualdirectory' is my category of the product and when the user visits just the category url I want to show him some information from root/realdirectory/index.php
Thank you in advance
Sounds pretty straight forward: when a request does not get resolved to an existing file or folder, then internally rewrite it to an "index document":
RewriteEngine on
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
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 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'm currently programming a website that, in a nutshell, loads its content dynamically via url parameters and php includes. The website is living under the root directory in a subfolder called "saischek".
The urls have one optional parameter: page, therefore the urls can for example look like this:
localhost/saischek/index.php?page=accounting
localhost/saischek/index.php
I would like to have that my urls look like this:
localhost/saischek/accounting
localhost/saischek/home <- if the url parameter _page_ isn't given
My .htaccess file is currently living in the subdirectory "saischek" and looks as follows:
RewriteEngine on
RewriteRule ^saischek/([a-zA-Z0-9_-]+)$ saischek/index.php?page=$1
The website is running on apache webserver and all necessary changes in "httpd.conf" file are done and working.
I'd say with the discussion in the comments to the question and your revision of the question you are nearly there, you found the solution yourself...
I would recommend to slightly change the main rule, also you will need to handle the "home" page you mentioned
RewriteEngine on
RewriteCond %{REQUEST_URI} !/saischek/index\.html$
RewriteRule ^/?saischek/home/?$ /saischek/index.php [QSA,END]
RewriteRule ^/?saischek/([a-zA-Z0-9_-]+)/?$ /saischek/index.php?page=$1 [QSA,END]
I wonder however why you want to use the "home" slug at all, why not simply use https://example.org/saischek instead of https://example.org/saischek/home? In that case you would try this variant:
RewriteEngine on
RewriteCond %{REQUEST_URI} !/saischek/index\.html$
RewriteRule ^/?saischek/?$ /saischek/index.php [QSA,END]
RewriteRule ^/?saischek/([a-zA-Z0-9_-]+)/?$ /saischek/index.php?page=$1 [QSA,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 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).
Try adding ? and changing + to * because in case the page is missing, the URL would not have them both.. (both / and page).
^saischek/([a-zA-Z0-9_-]+)/?([a-zA-Z0-9_-]*)$
I'm using XAMPP 7.2.6 on Windows and my root web folder is htdocs. I have the mod_rewrite module installed in Apache.
I have a website project in C:/xampp/htdocs/xyz
I want to use mod_rewrite to rewrite the URL of the root directory and index.php to a custom URL in the browser - index.php?action=viewFrontPage&pageId=2
.htaccess file
RewriteEngine on
RewriteRule ^/index.php? /index.php?action=viewFrontPage&pageId=2
This does nothing and I can't figure it out.
There are a few issues with your approach, but I will point out the first and most obvious:
Your current configuration
RewriteEngine on
RewriteRule ^/index.php? /index.php?action=viewFrontPage&pageId=2
does not express what you expect it to express and in fact that rule will never get applied. Why? Check the documentation! That is where you can read that the pattern will get matched against a relative path of a rewriting rule is implemented in a dynamic configuration file (".htaccess"). You try to match an absolute path.
That is why I would suggest this as a first step towards your goal (which is actually unclear from your question):
RewriteEngine on
RewriteRule ^/?index\.php$ /index.php?action=viewFrontPage&pageId=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 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).
That adapted rule will not work. This brings us to step 2... The issue you now face is that you have created an endless rewriting loop. You redirect /index.php to /index.php... not a good idea for obvious reasons. So you need to prevent such a loop, but for that you will need to be able to tell how the server should distinguish between the two situation. Are you able to?
This might be a start, but you will certainly need to adapt it:
RewriteEngine on
RewriteCond %{QUERY_STRING} !^action=viewFrontPage&pageId=2$
RewriteRule ^/?index\.php$ /index.php?action=viewFrontPage&pageId=2 [END]
Also also you wrote that you want to rewrite the "root directory", but it is unclear what you actually mean by that...
How to use htacess to do this addressing?
site.ru/catalog/category/tovar-1/
site.ru/catalog/category2/tovar439/
site.ru/catalog/category313/
site.ru/
How to make these links open also on this url
site.ru/dop/catalog/category/tovar-1/
site.ru/dop/catalog/category2/tovar439/
site.ru/dop/catalog/category313/
site.ru/dop/
This probably is what you are looking for:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/dop/
RewriteRule ^/?(.*)$ /dop/$1 [QSA,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).
how do I make a redirect this way
www.url.com/site/7/index.php => www.url.com/packets/index.php?id=7&url=index.php
currently used .htaccess
RewriteRule ^site/([0-9]+)/([0-9a-zA-Z-_]+)$ packets/index.php?id=$1&url=$2 [L,QSA]
need httaccess code to redirect this way
The issue with your own approach (your rewrite rule) is that it does not match the incoming request: [0-9a-zA-Z-_] does not match the literal full stop (".")...
I guess this is what you are looking for:
RewriteEngine on
RewriteRule ^/?site/(\d+)/(.+)/?$ /packets/index.php?id=$1&url=$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).