Mod_rewrite how to remove URI segment - apache

I have multiple languages on my web page. Here are link examples:
http://myweb.com/en/page_load/about_us
http://myweb.com/en/page_load/testing
http://myweb.com/de/page_load/about_us
http://myweb.com/de/page_load/testing
I want to make it shorter like this:
http://myweb.com/en/about_us
http://myweb.com/en/testing
http://myweb.com/de/about_us
http://myweb.com/de/testing
Currently I have this in my .htaccess file:
RewriteEngine On
RewriteCond $1 !^(index\.php|images|public|css|blogg|img|captcha|robots\.txt|sitemap.xml|resources)
RewriteRule ^(.*)$ /index.php/$1 [L]
Any suggestions?

You could do this with CodeIgniter's built-in routing. Try something like this in your routes.php config file.
$route['en/page_load/:any'] = "en/$1";
$route['de/page_load/:any'] = "de/$1";
That should give you what you are looking for.

If you just want to remove the page_load part, you can just do:
RewriteRule ^(.*)/page_load/(.*)$ $1/$2 [L]
If you want to merge both rules, I recommend you first to use REQUEST_URI as the RewriteCond variable (instead of $1, which depends on the RewriteRule), and also, if possible, to specify conditions on positive cases instead of negative cases (prefer not using ! for conditions). I think this way would be more maintainable.

Related

How to redirect URL using .htaccess with dynamic parameter?

I want to redirect
https://example.com/product-info/A100001
to
https://example.com/product-info/index/index/id/A100001
using htaccess redirect rule
A100001 will be dynamic like
A100001
A100002
A100003
A100004
....
I am trying this
RewriteEngine on
RewriteCond %{QUERY_STRING} product-info/A100001
RewriteRule ^$ /routing/index/index/id/? [L,R=301]
Source
Also tried other example but not working in my scnario
Anyone who expert in htacees rules can help me in this.
RewriteCond %{QUERY_STRING} product-info/A100001
RewriteRule ^$ /routing/index/index/id/? [L,R=301]
Your example URL contains a URL-path only, it does not contain a query string. The rule you've posted would redirect /?product-info/A100001 to /routing/index/index/id/.
Try something like the following instead:
RewriteRule ^(product-info)/(A\d{6})$ /$1/index/index/id/$2 [R=302,L]
The above would redirect a request of the form /product-info/A123456 to /product-info/index/index/id/A123456.
The $1 backreference simply contains product-info, captured from the RewriteRule pattern (saves repitition) and $2 contains the dynamic part (an A followed by 6 digits).
This is a 302 (temporary) redirect. Always test first with a 302 to avoid potential caching issues.
The order of directives in your .htaccess file is important. This rule will likely need to go near the top of the file, before any existing rewrites.
UPDATE:
redirection is working with your code, Can you please let me know the parameter pattern, I need the number from A452218 to A572217
Regex does not handle numeric ranges, only character ranges. If you specifically only want to match numbers in the stated range then you would need to do something (more complex) like this:
RewriteRule ^(product-info)/A(45221[89]|4522[2-9]\d|452[3-9]\d{2}|45[3-9]\d{3}|4[6-9]\d{4}|5[0-6]\d{4}|57[01]\d{3}|572[01]\d{2}|57220\d|57221[0-7])$ /$1/index/index/id/A$2 [R=302,L]
NB: The $2 backreference now only contains the dynamic number, less the A prefix, which is now explicitly included in the substitution string.

htaccess rule to remove parameters

struggling to come up with a .htaccess rule to remove parameters from it.
Essentially I want
http://www.example.com/page/competition.html?utm_medium=email&utm_campaign=11&utm_source=11&utm_term=11.gif
to rewrite to
http://www.example.com/page/competition.html
so, anything right from the /page/compeition.html is rewritten.
Grateful for any assistance. thanks in advance
In your question, you state that you want the long URI rewritten to the shorter one. Unfortunately, that isn't very clear. Assuming that you wish to simply remove the query string using a redirect, you can use the following:
RewriteCond %{QUERY_STRING} ^utm_medium=email&utm_campaign=([^&]+)&utm_source=([^&]+)&utm_term=([^&]+)$
RewriteRule ^ %{REQUEST_URI}? [R=302,L]
This is a generalised condition and rule that will check for any utm information in the query string. If it is present, then strip out the query string.
If you wish to only do this for page/competition.html, then change the rule to this:
RewriteRule ^(page/competition\.html)$ /$1? [R=302,L]
If you wish to simply remove any query string forming part of a request to page/competition.html, then you can use this instead of the above:
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(page/competition\.html)$ /$1? [R=302,L]
This basically tells Apache to check if the query string is not empty and, if it isn't, strip it out altogether.
To make the redirect permanent, change 302 to 301.
As a general rule, you should be able to use the following:
RewriteRule ^(.*)$ $1? [END,QSA]
The key in this rule is the trailing questionmark which will not appear in the output URL.
RewriteRule ^(.*)$ $1? [END,QSA] This rule was met, the new url is
http://www.example.com/page/competition.html
Try it out at the following:
http://htaccess.madewithlove.be/
Do you want that only for the specific page? Or more as a general approach?
RewriteRule ^page/competition.html?(.+)$ /page/competition.html [L,NC]
That would be the very easiest form to achieve it, but would only work for the exact filename. For a more general approach, you would need to add some more placeholders.

mod_rewrite doesn't work both directions

I get an original url:
www.mydomain.com/menu/?myid=29&mypage=pizza-hut.html
with the following mod_rewrite code:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^menu/([^/]*)/([^/]*)\.html$ /menu/?pid=$1&alias=$2 [L]
I get this nice url:
www.mydomain.com/menu/29/pizza-hut.html
so, both url above reference the same page...right!
now the real deal is,
WHY when I type the url,the original one:
www.mydomain.com/menu/?myid=29&mypage=pizza-hut.html
it doesn't redirect to
www.mydomain.com/menu/29/pizza-hut.html
it keeps its original one in the address bar, is there any line I should add?
Essentially, all the links you generate should be the nice urls. Don't have links like /menu/?myid=29&mypage=pizza-hut.html in any of your pages. Use the clean URLs that you've ensured to route correctly on the back end that look like this: www.mydomain.com/menu/29/pizza-hut.html
The rules that you have rewrite on the server the nice looking urls to what your content understands (e.g. /menu/?myid=29&mypage=pizza-hut.html). That's the most important part. If you want to correct all the direct requests for the ugly URLs, you need to first make sure all your pages start using the nice looking ones, then you can maybe do something like this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /menu/?\?myid=([^&]+)&mypage=([^&\ ]+)
RewriteRule ^menu/?$ /menu/%1/%2? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /menu/?\?myid=([^&]+)&mypage=([^&\ ]+)
RewriteRule ^menu/?$ /menu/%1/%2.\html? [R=301,L]
Note the ? following the .\html

Is it possible to capture a variable from an Apache RewriteCond pattern match?

Using mod_rewrite I'd (ideally) like to capture a spacific ID from a URL in a variable from a RewriteCond pattern match for (e.g.)
/index.php?option=com_content&view=article&id=984&Itemid=131
to use it later in a RewriteRule.
I don't have a problem writing the pattern, but can't find out if this is actually possible, despite reading the docs. Has anyone else tried this or know it to be impossible?
The site is built on Joomla 1.6 (which adds IDs to SEO-friendly URLs), and I'm trying to avoid the overhead of something like HP Router just to change one particular group of URLs.
The following should work. You can use a % sign to access the groups from the RewriteCond
RewriteCond %{QUERY_STRING} Itemid=([0-9]*)$
RewriteRule (.*) /some/page/%1? [L,R=301]
Hope this helps.

Rewrite rule on multiple variables using Apache mod_rewrite

I would like to know how can I rewrite www.site.com?lang=lv&type=1&mode=2 into www.site.com/lv/1/2 using Apache mod_rewrite options.
Assuming that you actually want to rewrite /lv/1/2 to ?lang=lv&type=1&mode=2 (I see no reason to do the opposite) and that no other rewrites are active, this should do the trick:
RewriteRule ^/([^/]*)/([^/]*)/([^/]*)$ ?lang=$1&type=$2&mode=$3 [L]
Also; you'd be better off replacing those magic numbers with more useful information if you want to include them in your URI.
Edit: If it really is the opposite you'd like to do, see the answer by Matt S.
Basic rewrite:
RewriteEngine On
RewriteRule http://www.site.com/?lang=(.+?)&type=(\d+?)&mode=(\d+?) http://www.site.com/$1/$2/$3 [L,R=permanent]
Edit: Changed rewrite flags to force a permanent redirect
You need to handle the URI path and query separately. If the parameters appear in that very same order, you can do this:
RewriteCond %{QUERY_STRING} ^lang=([^&]+)&type=([^&]+)&mode=([^&]+)$
RewriteRule ^$ /%1/%2/%3? [L,R=301]
Otherwise you will need to put them in the right order before using this rule or take each parameter at a time.