Index page redirection to /?lang=ro - .htaccess

How can I redirect my website from example.com to example.com/?lang=ro ? I want to set default language to "ro" because it changes only on flag click, I think that i have to set lang variable to "ro", but it doesn't work.

You need to check if query string exists in request URL, if not, then you can append it in your URL.
Check below code :
RewriteCond %{QUERY_STRING} !lang
RewriteRule (.*) $1?lang=ro [R=302,QSA,L]
This will append query string lang=ro and redirect if query string not exist in request URL.

Related

Using htaccess how do I RewriteRule/RewriteCond with no filename?

Hoping this isn't a duplicate, done a lot of looking and I just get more confused as I don't use .htaccess often.
I would like to have some pretty URLs and see lots of help regarding getting information where for example index.php is passed a parameter such as page. So I can currently convert www.example.com/index.php?page=help to www.example.com/help.
Obviously I'm not clued up on this but I would like to parse a URL such as www.example.com/?page=help.
Can't seem to find much info and adapting the original I am obviously going wrong somewhere.
Any help or pointers in the right direction would be greatly appreciated. I'm sure its probably stupidly simple.
My alterations so far which do not seem to work are:
RewriteCond %{THE_REQUEST} ^.*/?page=$1
RewriteRule ^(.*)/+page$ /$1[QSA,L]
Also recently tried QUERY_STRING but just getting server error.
RewriteCond %{QUERY_STRING} ^page=([a-zA-Z]*)
RewriteRule ^(.*) /$1 [QSA,L]
Given up as dead to the world so thought I would ask. Hoping to ensure the request/url etc starts ?page and wanting to make a clean URL from the page parameter.
This is the whole/basic process...
1. HTML Source
Make sure you are linking to the "pretty/canonical" URL in your HTML source. This should be a root-relative URL starting with a slash (or absolute), in case you rewrite from different URL path depths later. For example:
Help Page
2. Rewrite the "pretty" URL
In .htaccess (using mod_rewrite), internally rewrite the "pretty" URL back to the file that actually handles the request, ie. the "front-controller" (eg. index.php, passing the page URL parameter if you wish). For example:
DirectoryIndex index.php
RewriteEngine On
# Rewrite URL of the form "/help" to "index.php?page=help"
RewriteRule ^[^.]+$ index.php?page=$0 [L]
The RewriteRule pattern ^[^.]+$ matches any URL-path that does not include a dot. By excluding a dot we can easily omit any request that would map to a physical file (that includes a file extension delimited by a dot).
The $0 backreference contains the entire URL-path that is matched by the RewriteRule pattern.
The DirectoryIndex is required when the "homepage" (root-directory) is requested, when the URL-path is otherwise empty. In this case the page URL parameter is not passed to our script.
3. Implement the front-controller / router (ie. index.php)
In index.php (your "front-controller" / router) we read the page URL parameter and serve the appropriate content. For example:
<?php
$pages = [
'home' => '/content/homepage.php',
'help' => '/content/help-page.php',
'about' => '/content/about-page.php',
'404' => '/content/404.php',
];
// Default to "home" if "page" URL param is omitted or is empty
$page = empty($_GET['page']) ? 'home' : $_GET['page'];
// Default to 404 "page" if not found in the array/DB of pages
$handler = $pages[$page] ?? $pages['404'];
include($_SERVER['DOCUMENT_ROOT'].$handler);
As seen in the above script, the actual "content" is stored in the /content subdirectory. (This could also be a location outside of the document root.) By storing these files in a separate directory they can be easily protected from direct access.
4. Redirect the "old/ugly" URL to the "new/pretty" URL [OPTIONAL]
This is only strictly necessary (in order to preserve SEO) if you are changing an existing URL structure and the "old/ugly" (original) URLs have been exposed (indexed by search engines, linked to by third parties, etc.), otherwise the "old" URL (ie. /index.php?page=abc) is accessible. This is the same whenever you change an existing URL structure.
If the site is new and you are implementing the "new/pretty" URLs from the start then this is not so important, but it does prevent users from accessing the old URLs if they were ever exposed/guessed.
The following would go before the internal rewrite and after the RewriteEngine directive. For example:
# Redirect "old" URL of the form "/index.php?page=help" to "/help"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/index\.php$ [OR]
RewriteCond %{QUERY_STRING} ^page=([^.&]*)
RewriteRule ^(index\.php)?$ /%1 [R=301,L]
The check against the REDIRECT_STATUS environment variable prevents a redirect-loop by not redirecting requests that have already been rewritten by the later rewrite.
The %1 backreference contains the value of the page URL parameter, as captured from the preceding CondPattern (RewriteCond directive). (Note how this is different to the $n backreference as used in the rewrite above.)
The above redirects all URL variants both with/without index.php and with/without the page URL parameter. For example:
/index.php?page=help -> /help
/?page=help -> /help
/index.php -> / (homepage)
/?page= -> / (homepage)
TIP: Test first with 302 (temporary) redirects to prevent potential caching issues.
Comments / improvements / Exercises for the reader
The above does not handle additional URL parameters. You can use the QSA (Query String Append) flag on the initial rewrite to append additional URL parameters on the initially requested URL. However, implementing the reverse redirect is not so trivial.
You don't need to pass the page URL parameter in the rewrite. The entire (original) URL is available in the PHP superglobal $_SERVER['REQUEST_URI'] (which also includes the query string - if any). You can then parse this variable to extract the required part of the URL instead of relying on the page URL parameter. This generally allows greatest flexibility, without having to modify .htaccess later.
However, being able to pass a page URL parameter can be "useful" if you ever want to manually rewrite (override) a URL route using .htaccess.
Incorporate regex (wildcard pattern matching) in the "router" script so you can generate URLs with "parameters". eg. /<page>/<param1>/<param2> like /photo/cat/large.
Reference:
https://httpd.apache.org/docs/2.4/rewrite/
https://httpd.apache.org/docs/2.4/rewrite/intro.html
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
RewriteCond %{QUERY_STRING} ^page=([^&]+)
RewriteRule ^$ /%1? [R=302,L]
Can't delete and didn't want to waste anyones time responding.

How to rewrite URL GET parameters

I have a URL
https://example.com/cart?prdid=223
I want to redirect it to
https://example.com/cart/223
using .htaccess
i've tried to use
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)prdid=(.*)$
RewriteRule ^cart/(.*)$ /cart/$1?prdid=%1 [L,R]
But it does not work.
Rewrite rules have the pattern to match first, then the result you want.
The pattern you need to match is the current URL, which just ends "/cart", with no extra slash or word on the end of it, so instead of cart/(.*)$ you just want cart$
Then the result you want has the ID directly in the URL, not in the query string, and there's nothing for $1 to refer to, only %1 from the RewriteCond line. So instead of /cart/$1?prdid=%1 you just want /cart/%1
Once you've fixed that, the browser will redirect to the new URL. To actually make that URL work, you'll probably need a second rule, without the R flag, to tell Apache what to do when it sees the "pretty" URL. That one will have cart/(.*)$ as the pattern to match, but no condition on the query string, and $1 in the result part, not %1

mod_rewrite pass variable and

What I would need to do is to pass 1 variable as variable, and the rest as a rest of URL intact, so I can get them by $_GET in php later. The below does not work:
RewriteRule ^store/([a-zA-Z0-9\_\-]+).html?(.*)/?$ store.php?var1=$1&$2 [L]
Possible Links could be:
store/products.html
store/products.html?sort=asc&price=down
store/products.html?price=down&here_we_can_have_a_lot_of_different_params_in_whatever_order
Basically, just take this $var1 and the rest forward to URL? How can I do that?
P.S. I think I found a solution:
RewriteRule ^store/([a-zA-Z0-9\_\-]+).html?(.*)/?$ store.php?var1=$1&%{QUERY_STRING} [L]
The problem is your premise as to how a RewriteRule works. A RewriteRule only matches against the URI path (in apache terms the Request URI) and not against the query string. This means the (.*)/?$ at the end of your regex only works because it can reduce to just '$' (or the end of the string) and the ? before it just means the regexp will match .htm as well as .html
The simpler version of your rule is as follows:
RewriteRule ^store/([a-zA-Z0-9\_\-]+).html store.php?var1=$1 [L,QSA]
The QSA stands for Query String Append, which simply adds back any existing query string to the rewritten URL.

Why does my RewriteRule not work when there is a `?` in the URL

I am learning how to write regular expressions for .htaccess redirects.
So far I've managed to figure out everything I needed, except for a couple of regular expressions which don't behave as I expected. I am testing my regular expressions using a desktop application, and they work fine there, but not in the .htaccess file.
FYI: The RewriteBase is set to /site/
This is the incoming URL:
/site/view-by-tag/politics/?el_mcal_month=3&el_mcal_year=2009
I want to grab "politics" and redirect to /site/tags/politics/
Here is what I used:
RewriteRule ^view-by-tag/([a-zA-Z\-]+)/([a-zA-Z0-9\-\/\.\_\=\?\&]+) /tags/$1/ [R=301,L]
I added the capture of all the characters after politics because I am having the issue that when there is a ? in the URL the redirect does not work, and I can't figure out why. In the URL given above, if I remove the ? it works fine, but if the ? is in there, nothing happens. Is there a reason for this?
The same thing happens when I try to capture 307 from /site/?option=com_content&view=article&id=307&catid=89&Itemid=55
I used this regular expression, article&id=([0-9]+) /?p=$1 [R=301,L] but again, when there is a ? in the URL it stops the redirect for doing anything.
What is the reason for that?
The .htaccess file in question is on a Wordpress blog (3.4.1)
The point that you've missed is that the rewrite engine splits the URI into two parts: the REQUEST_URI and the QUERY_STRING. The query string part isn't used in the rule match string so there is no point in constructing rule regexp patterns to look for it.
You can probe and pick out parameters from the query string by using rewrite conditions and condition regexps to set %N variables.
By default the query string is appended to the output substitution string unless you have a ?someparam in it -- in which case it is ignored unless you used the [QSA] (query string append) parameter.
The way that you'd pick up the id in /site/?option=com_content&view=article&id=307&catid=89&Itemid=55 is to use something like:
RewriteCond %{QUERY_STRING} \bid=(\d+)
Before the rule and this would set %1 to 307. Read the rewrite documentation for more general discussion of how to do this.
The query string is must be processed separately in a RewriteCond if you need to manipulate it, and should not be matched inside the RewriteRule Instead, just match the request not including the query string, and use QSA to append the query string onto the redirect:
RewriteRule ^view-by-tag/([A-Za-z-]+)/?$ /tags/$1/ [R=301,L,QSA]
# OR, if you don't want the rest of the query string appended, put a `?` onto
# the redirect to replace it with nothing
RewriteRule ^view-by-tag/([A-Za-z-]+)/?$ /tags/$1/? [R=301,L]
Actually, the QSA may not be needed in a R redirect - I think that the default behavior is to pass the query string with the redirect.
If you need to capture 307 from the query string, do it in a RewriteCond and capture in %1:
# Capture the id in %1
RewriteCond %{QUERY_STRING} id=([\d]+)
# Redirect everything to /, pass %1 into p
RewriteRule . /?p=%1 [LR=301,L]

.htaccess redirect based on query string and remove query string

I'm in need to have redirect instruction to redirect the old URL with query string to new URL without query string. And the new URL depends on value of query string. For example:
I want:
http:// www.mydomain.com/product.php?id_product=AAA
Will redirect to:
http:// www.mydomain.com/product-name-for-product-number-1
And
http:// www.mydomain.com/product.php?id_product=BBB
Will redirect to:
http:// www.mydomain.com/this-is-different-product-name-for-product-number-2
Thanks in advance.
you can do this type of redirection:
from: http:// www.mydomain.com/product-name-for-product-number-AAA
to: http:// www.mydomain.com/product.php?id_product=AAA&name_product=product-name
from: http:// www.mydomain.com/this-is-different-product-name-for-product-number-2
to: http:// www.mydomain.com/product.php?id_product=BBB&name_product=this-is-different-product-name
you cannot retrieve dinamically the product name from htaccess file, the only method to do this is:
RewriteRule ^(.+)-for-product-number-(.+)$ ^product.php?id_product=$2&name_product=$1
or you need to create a rewriterule for each product you have (you can generate the .htaccess from a script that retrieve from db all products id & name):
RewriteRule ^product-name-for-product-number-1$ product.php?id_product=AAA
RewriteRule ^this-is-different-product-name-for-product-number-2$ product.php?id_product=BBB
It seems to me you cannot perform a redirection based on a query string. So you would have to add [QSA] flag and redirect to a script which will be able to redirect thanks to the query string.

Resources