First Of all i want to tell that I am using XAMPP.
I have access to a folder in htdocs folder named mywebsite.
I want to wrote some sort of URL Masking Script in htaccess by which when every i visit localhost/mywebsite/index.php , content shown from another domain withour changing the URL.
I wrote below code yet but not able to do that.
RewriteEngine On
RewriteRule ^(.*)$ https://www.google.co.in/$1 [P]
It showing me
Server error!
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there was an error in a CGI script.
Kindly Clear me, Is it possible or not.
& if yes then how it will be.
If you have enabled the http servers proxy module as you confirmed in the comments to the question then you also need to enable the SSL proxy feature it offers. So I guess this is what you are looking for:
RewriteEngine On
SSLProxyEngine On
RewriteRule ^(.*)$ https://www.google.co.in/$1 [P]
If that external resource does not only consist of a single document, but has relative links, then you also need a reverse proxy, otherwise the client will break out of your proxy:
RewriteEngine On
SSLProxyEngine On
ProxyPass / https://www.google.co.in/
ProxyPassReverse / https://www.google.co.in/
However that is something you cannot do inside dynamic configuration files, you need to place such directives inside the http servers host configuration.
And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).
Related
Hi i tried lot of examples but nothing is working for me.
I need to redirect
From : example.com/category.php?category_id=1&category=பொது
To : example.com/category/1/பொது/
I am using following htaccess
DirectoryIndex index.php
AddDefaultCharset utf-8
RewriteEngine On
RewriteCond %{QUERY_STRING} category_id=$1&category=$2
RewriteRule ^category\.php$ /$1/$2/? [L,R=301]
It's redirect to something like this
example.com/category/%25E0%25AE%25AA%25E0%25AF%258A%25E0%25AE%25A4%25E0%25AF%2581
also showing page not found. Someone please help me. Thanks.
This probably is what you are looking for:
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)category_id=([^&]+)&category=([^&]+)(?:&|$)
RewriteRule ^/?category\.php$ /category/$1/$2 [QSD,END,R=301]
RewriteRule ^/?category/([^/]+)/([^/]+)/? /category.php?category_id=$1&category=$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 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 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).
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
I want to redirect
https://example.com/uploads/cms_files/GENERAL-TERMS-AND-CONDITIONS.pdf?docs/GENERAL_TERMS_AND_CONDITIONS.pdf
to
https://exmaple.com/uploads/cms_files/GENERAL-TERMS-AND-CONDITIONS.pdf
by using .htaccess file in core php but it's not redirecting because I have ?query in my URL but there is no key for the value after question mark.
Can someone help me how to redirect this URL in .htaccess?
The direct implementation of what you ask would be this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^docs/GENERAL_TERMS_AND_CONDITIONS\.pdf$
RewriteRule ^/?uploads/cms_files/GENERAL-TERMS-AND-CONDITIONS\.pdf https://example.com/uploads/cms_files/GENERAL-TERMS-AND-CONDITIONS.pdf [R=301,QSD]
That rule will work likewise in the http servers host configuration and in a dynamic configuration file (.htaccess). If you decide to use a dynamic configuration file, then you have to take care that its interpretation is enabled at all (AllowOverride directive), that such file is readable by the http server process and that it is located in the right location inside the http hosts document folder.
However why would you have to rewrite that at all? If an object exists that can be requested using that URL, then the query string can simply be ignored...
And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only supported as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).