.httaccess with redirect - php

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).

Related

Rewrite root directory and index.php to custom URL in browser

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 rewrite a URL using PHP and .htaccess with multiple values

newprojectinfo.com/property-single.php?city=Bangalore&area=Rajaji-Nagar&project_name=Prestige-West-Woods
I need to rewrite that URL like:
newprojectinfo.com/Bangalore/Rajaji-Nagar/Prestige-West-Woods
Please let me know how to write .htaccess for this URL.
//-----The below anchor tag is my code--------
<a href="property-single.php?city=<?php echo rtrim(str_replace(' ', '-', $slideProjects['city_name']))?>&area=<?php echo str_replace(' ', '-', $slideProjects['area_name'])?>&project_name=<?php echo str_replace(' ', '-', $slideProjects['project_name'])?>">
Your question is vague about what your actual situation is, but as a direct answer to a question this probably is what you are looking for:
RewriteEngine on
RewriteRule ^/?([^/]+)/([^/]+)/([^/]+)/?$ /property-single.php?city=$1&area=$2&project_name=$3 [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 use htacess to do this addressing?

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).

insert extension into files without extension by htaccess

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).

Htaccess pagination and multiple query strings

I'm trying to setup a redirect in order to showcase a business page, with the listings on that page, paginated.
This is how I want the URL to look like:
https://www.propertypost.lk/company/colomborent/1
Where the last number in the URL, is the page number.
I've been able to get this to work: https://www.propertypost.lk/company/colomborent
But I can't seem to figure out how to include the pagination part (which shouldn't be mandatory).
This is how the URL looks without htaccess:
https://www.propertypost.lk/agent-business-page.php?url=colomborent&page=1
And here is my htaccess rewrite rule that works:
RewriteRule ^company/([^\s]+[\w])/?$ agent-business-page.php?url=$1
And this is what I tried which does not work...:
RewriteRule ^company/([^\s]+[\w])/([^\s]+[\w])/?$ agent-business-page.php?url=$1&page=$2
Thanks for all the help!
You want \d instead of \w to capture the numerical pagination number argument. So something like:
RewriteEngine on
RewriteRule ^/?company/(\w+)/?$ /agent-business-page.php?url=$1 [END]
RewriteRule ^/?company/(\w+)/(\d+)/?$ /agent-business-page.php?url=$1&page=$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).

Categories