.htaccess rule for recursive subfolder links - .htaccess

I want one or a few rule for many subfolders url. I don't want to write words like -blog- or -shop- into the rule.
https://www.example.com/en/
https://www.example.com/en
https://www.example.com/en/blog/
https://www.example.com/de/shop
https://www.example.com/en/shop/keyboard
https://www.example.com/de/shop/cpu/amd5/
I want output like this:
print_r($_GET) outputs:
Array ( [lang] => en )
Array ( [lang] => en )
Array ( [lang] => en [seo1] => blog )
Array ( [lang] => de [seo1] => shop )
Array ( [lang] => en [seo1] => shop [seo2] => keyboard )
Array ( [lang] => en [seo1] => shop [seo2] => cpu [seo3] => amd5 )

This would be a simple approach that also allows for exceptions and later flexibility:
RewriteEngine on
RequestCond %{REQUEST_URI} !/router\.php
RewriteRule ^/?([^/]+)/?$ /router.php?lang=$1 [END]
RewriteRule ^/?([^/]+)/([^/]+)/?$ /router.php?lang=$1&seo1=$2 [END]
RewriteRule ^/?([^/]+)/([^/]+)/([^/]+)/?$ /router.php?lang=$1&seo1=$2&seo2=$3 [END]
RewriteRule ^/?([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /router.php?lang=$1&seo1=$2&seo2=$3&seo3=$4 [END]
Here /router.php is just an example, you will need to use whatever logic you have implemented internally.
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

.htaccess remove multiple parameters from url

I need to remove all GET parameters from URL and get them by $_GET. The structure will be like this
From:
example.com/?section=DP&id=366
OR
example.com/index.php?section=DP&id=366
To:
example.com/something-dynamic-from-db,DP-366.html
How can i get this?
No, you cannot have a literal comma (",") in a URL, it is an invalid character in there. You could go for https://example.com/something-dynamic-from-db/DP-366.html though. This should point you into the right direction:
RewriteEngine on
// some other. more specific rewriting rules here
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RequestRule ^/?.+/(\w+)-(\d+)\.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 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 want to remove part of the name from a url using htaccess

Hi Friends I Want to create new page as per country name dynamically i create them as per query and links also working but i got a problem i want to remove page name from url where am put all this code
now Links Are Like
https://www.example.com/result/canada....................After use Htaccess This Url Is Working Fine
orignal Url is (https://www.example.com/result.php?canada)
But
I Want This Url Like
https://www.example.com/canada
Please Tell Me The Right Way To Do with Htaccess
My Site Is in Core Php. Not In Wordpress
You want something like that:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(\w+)/?$ /result/$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).

If Condition in RewirteRule .htaccess

I am migrating an old website and as part of the task is the migration of the old URLs to the new format.
In the old format, we have lang2 that identifies ENglish, and in the new webpbage I see that the URL has changed to "en".
An old url looks like:
page/lang2/1111/some-title.html
I can extract the variables I need (pattern="page/([^/])/([^/])/([^/]*).html" from the above URL and handle a forward in a Rewrite Rule. What seems hard to do is alter the variable holding "lang2" into "en".
Any one has an idea how to do this?
The new URL should loook like /en/title.id and a full rule would have been:
ReWriteRule ^page\/([^/]*)\/([^/]*)\/([^/]*).html$ /$1/$3.$2 [L]
Problem is the $1 should be changed to "en" if lang2 and another code if "lang1"
So there are only two options, "lang1" and "lang2"? Then simply make two separate rules:
RewriteEngine on
RewriteRule ^/?page/lang2/(\d+)/(.*)\.html$ /en/$2.$1 [END]
RewriteRule ^/?page/lang1/(\d+)/(.*)\.html$ /xx/$2.$1 [END]
Note: "xx" obviously is meant as a placeholder, you did not specify what you mean by "another code".
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 write '?' symbol in htaccess

I am using htaccess. I try to pass a value in url like '/?'
like "domain.com/?detail.html"
I am retrieving the value in htaccess like
RewriteRule ^?(.*).html$ index.php?key=detail
But it returns only 'detail'. Symbol '?' not accepted. I need the the value '?detail.html'
Is there any solution?
You did not understand how URLs are build. The ? has a special meaning in it. It separates URL and query string. You cannot capture or detect it in a rewriting rule since it is not part of the URL.
If that URL really is what you get as a request, then this would rewrite it as you want:
RewriteEngine in
RewriteCond %{QUERY_STRING} ^(\w+)\.html$
RewriteRule ^/?$ /index.php?key=%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 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 want replace url use htaccess

How to replace url use htaccess ? My url :
domain.com/xem/?i=example
i want replace to
domain.com/xem/example
I want to replace these links without error, please help me!
An internal rewrite to /?i=example makes little sense (I assume that is what you actually ask, domain.com?i=example makes even less sense, or do you really have a folder nameddomain.com on your system?). Instead you should rewrite to your actual router script or logic, so something like
RewriteEngine on
RequestCond %{REQUEST_FILENAME} !-f
RequestCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(\w+)/?$ /index.php?i=$1 [END]
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.
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.
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).

Resources