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).
Related
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...
I need three things in the htaccess file:
Load a php file, if specific keywords are in the url
Pass the last word (after slash) of url to php as a parameter
Keep the origin URL in browser adress bar
e.g.
https://example.com/anyString/anyKeyword1/anyKeyword2/anyString/volvo
Or more specific examples with the keywords 'repaircars' and 'offering':
https://example.com/testsystem/repaircars/offering/volvo
https://example.com/repaircars/offering/volvo
So, whenever the keywords 'repaircars' and 'offering' are in the url, the htaccess file should load a file named
'show.php'
For this, I wrote the in the htaccess file:
RewriteRule ^repaircars/[^/]*(offering)[^/]*/ https://example.com/testsystem/show.php [L,R=301]
This is working fine. It's loading the show.php, if the keywords 'repaircars' and 'offering' are in the url.
But:
How can the file show.php receive the word after the last slash in url (here the last word is: volvo) as parameter?
How to keep the origin url in browser address bar and anyway processing the file show.php ?
Is this possible at all?
I would suggest a completely different approach:
RewriteEngine on
RewriteCond %{REQUEST_URI} repaircars
RewriteCond %{REQUEST_URI} offering
RewriteRule /(\w+)$ /show.php?brand=$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 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).
There are a few SO questions about mod_rewrite such as here and here, but so far I cannot find one that works with my example.
I have an Apache webserver running my PHP website at the root var/www/html. at /var/www/html/news I have a .htaccess file which will be used to convert ugly $_GET filled url string into SEO friendly urls.
My current url looks like this:
https://mywebsite.com/news/article.php?id=2&title=myfirstarticle
I would like my url to look like this:
https://mywebsite.com/news/2/myfirstarticle
Here is my .htaccess file, currently redirecting to the indended url structure.
RewriteEngine on
RewriteRule ^([^/]*)/([^/]*)$ /article.php?id=$1&title=$2
So far my URL is not being changed to the SEO friendly URL, what am I missing?
You need to handle both directions: the external redirection and the internal rewriting:
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)id=(\d+)&title=([^&]+)(?:&|$)
RewriteRule ^article.php$ /news/%1/%2 [R=301]
RewriteRule ^([^/]+)/([^/]+)$ /article.php?id=$1&title=$2 [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 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).
Try the below htaccess rule:
RewriteEngine On
RewriteRule ^news/([^/]*)/([^/]*)$ /news/article.php?id=$1&title=$2