Rewrite index.php to SEF URL without query params

Hello,
A really old site has URLs similar to this:

index.php?option=com_content&view=frontpage&Itemid=1
index.php?option=com_content&view=article&id=9&Itemid=8

All pages are indexed based on query parameters. I am lunching a new site and I want to 301 redirect all indexed pages to search engine friendly urls.

This is my Caddyfile testing it locally:

localhost:8080
gzip
log access.log

rewrite /index.php {
    if {query} match view=frontpage
    to /test
}

rewrite /index.php {
    if {query} match view=demo
    to /demo
}

Rewrite works but query parameters are appended to the URL after rewrite. My URL looks like this /test/?option=com_content&view=frontpage&Itemid=1

Is there a way to rewrite without the queries ?

I also tried using redir like this:

redir 301 {
	if {query} match view=frontpage
	/index.php /test
}

redir 301 {
	if {query} match view=demo
	/index.php /demo
}

Redir does not include the query parameters if it is just one redirect, but I get an error Error during parsing: rule with duplicate 'from' value: /index.php -> /test.

Any help appreciated :wink:

Yeah, I thought there was an open issue about this but I can’t find it now. This is on my TODO list; redirects shouldn’t be duplicates if they have if statements, even if the path is the same.

1 Like

Is something like this supposed to work or each if MUST be in its own redir block?

redir 301 {
	if {query} match view=frontpage
	/index.php /test

	if {query} match view=demo
	/index.php /demo
}

Can I create an issue for duplicate redirects with if statement?

Each one needs to be in its own redir block. Then that should work, but it doesn’t currently.

The solution to this right now is a little messy.

rewrite {
  if {query} has view=frontpage # use substring 'has' instead of regex 'match'; faster
  to /redir-to-test/
}
redir 301 {
  if {rewrite_uri} is /redir-to-test/
  /redir-to-test/ /test
}

rewrite {
  if {query} has view=demo
  to /redir-to-demo/
}
redir 301 {
  if {rewrite_uri} is /redir-to-demo/
  /redir-to-demo/ /demo
}

Thanks! Messy or not I will use it.
I have to redirect around 30 pages like this. Do you think it can cause any other drawbacks (slower server response or something else) beside the messy part?

Nah, should be pretty quick - I’d bet (not based on any actual benchmarks, mind) that 100s of these probably won’t have a big impact (unless you’re using match instead of has).

1 Like

This part did not work for some reason if {rewrite_uri} is /redir-to-demo/. The rewrite worked but then it did not redir.

What I tried was this:

rewrite {
	if {query} has view=demo
	to /redir-to-demo/
}
redir 301 {
	if {query} has view=demo
	/redir-to-demo/ /demo
}

rewrite {
	if {query} has view=test
	to /redir-to-test/
}
redir 301 {
	if {query} has view=test
	/redir-to-test/ /test
}

It worked. I am not very good with servers and I really can’t give you good explanation why if {rewrite_uri} is /redir-to-demo/ did not work. It seems a better solution.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.