Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


apache modrewrite rules ... going nuts. help needed
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

apache modrewrite rules ... going nuts. help needed

Hello everyone.
I am going nuts here (cannot get it right no matter how).

There will be update to my simple website soon, so old php links (mysql) will not work. New site is created using HUGO, so it will be simple static pages.

Since google have cached old links I want to redirect those soon non-existing links to new locations, so those who come to site from google will be redirected to new locations.

Trying to do it in .htaccess with modrewrite using apache server.

What I want is exactly following for like 15 URLs generated by old php mysql engine:

Non-existing http://example.com/index.php?option=com_content&view=article&id=2&Itemid=12&lang=en should be redirected to http://example.com/
Non-existing http://example.com/index.php?option=com_content&view=article&id=2&Itemid=12&lang=lv should be redirected to http://example.com/

Non-existing http://example.com/index.php?option=com_content&view=article&id=4&Itemid=5&lang=en should be redirected to http://example.com/newlocation/
Non-existing http://example.com/index.php?option=com_content&view=article&id=4&Itemid=5&lang=lv should be redirected to http://example.com/newlocation/

There are more URLs like those, where only id and Itemid and lang options changes.

Closest code from google I could found (and which works) is this:

Options -Indexes -MultiViews
RewriteEngine on

Redirect direct requests for "index.php?lang=xyz" to "/xyz/"

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^lang=([a-z]{2,3})$
RewriteRule ^(index.php)?$ /%1/? [R=301,L]

How to adopt that example for my case, I'm strugling to understand.
Help, please.

Comments

  • kkrajkkkrajk Member
    edited June 2021

    -

  • yoursunnyyoursunny Member, IPv6 Advocate

    I rebuilt my website in 2017, which included switching two sections of the website into Jekyll and Hexo.
    I solved the redirects problem with a custom 404 page.

    I have a PHP script as 404 handler, which could include all kinds of logic.
    This is easier than fighting with rewrite rules.

    My old website also contained URIs like /study/EI209/?topic=HDD that should now redirect to /study/EI209/HDD.htm, but /study/EI209/ itself is still a static page so it wouldn't trigger 404 handler.
    I solved this problem by invoking the same PHP script whenever the request URI contains any query string.

  • Daniel15Daniel15 Veteran
    edited June 2021

    I'd just create an index.php page that checks $_GET['id'] and does a 301 redirect to the right place. Or put it at the very top of your new index.php in case you're still using PHP for whatever reason Just noticed you mentioned that you're using Hugo, so ignore this last sentence.

    Remember that Cool URIs Don't Change (https://www.w3.org/Provider/Style/URI) so for the new site, try to think hard about the URL structure so that you don't have to change it ever again :)

    Thanked by 1yoursunny
  • JabJabJabJab Member

    @moodwriter said: What I want is exactly following for like 15 URLs generated by old php mysql engine:

    https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect

    15 urls seems like easy manual job - no need to use RewriteCond if you can't grasp that
    - just hardcode those redirects with simple Redirect or basic RedirectMatch.

  • jetchiragjetchirag Member
    edited June 2021

    @moodwriter said: RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteCond %{QUERY_STRING} ^lang=([a-z]{2,3})$
    RewriteRule ^(index.php)?$ /%1/? [R=301,L]

    You are limiting query by adding that carat. Try this:

    RewriteEngine on
    RewriteCond %{QUERY_STRING} lang=([a-z]{2,3})$
    RewriteRule . /%1/? [R=301,L]
    
    $ curl -I "https://XXX/index.php?option=com_content&view=article&id=4&Itemid=5&lang=xyz" | grep location
    
    location: https://XXX/xyz/
    
Sign In or Register to comment.