I'm new to this, please have grace...

So I understand changing the .htaccess file to let users type in a new link, and then it reference the old page as seen here:
I want to access 127.0.0.1/site/result.php?par1=var1&par2=var2&par3=var3, by typing in 127.0.0.1/site/var1-var2-var3.html

.htaccess file:
Options +FollowSymLinks
RewriteEngine On

RewriteRule ^(.*)_(.*)_(.*)\.html$ result.php?var1=$1&var2=$2&var3=$3


it works... but now, how do I automatically when I submit the form, get the new link to display? Or in other words, how do when we type in 127.0.0.1/site/result.php?par1=var1&par2=var2&par3=var3 be redirected to 127.0.0.1/site/var1-var2-var3.html?

Any help would be appreciated!

Dani AI

Generated

A couple of quick clarifications that should make this work more predictably.

First, the pattern in the example and the sample URL don’t match (one uses underscores, the other hyphens). That alone will break matching. Also, ’s advice about using a redirect flag can be useful — but if you put that flag on the rule that converts the pretty URL into the script, you’ll end up redirecting the pretty URL back to the query-string form (the reverse of what you want). There are three reliable ways to get the browser to show the pretty URL after form submission:

  • Have the form submit directly to the pretty URL (build the slug client- or server-side before submit).
  • Process the form on the script, then do a server-side redirect to the pretty URL (Post/Redirect/Get pattern).
  • Add a separate rule that detects the query-string URL and redirects to its pretty counterpart.

The server-side redirect approach is simple and safe. Sanitize and slugify each part, build the hyphen-separated filename, then redirect. Example (server-side):

function slugify($s){
  $s = iconv('UTF-8', 'ASCII//TRANSLIT', $s);
  $s = preg_replace('/[^A-Za-z0-9]+/', '-', $s);
  return strtolower(trim($s, '-'));
}

$parts = array_map('slugify', [$_REQUEST['par1'] ?? '', $_REQUEST['par2'] ?? '', $_REQUEST['par3'] ?? '']);
$slug = implode('-', array_filter($parts)) . '.html';
header('Location: /site/' . $slug, true, 302); // use 302 while testing
exit;

Notes and troubleshooting:

  • Test with a temporary redirect first, then switch to permanent when confident.
  • Avoid redirect loops by checking whether the current URL is already the pretty one.
  • If you need to preserve POST data, use session storage before the redirect or submit with GET.
  • If rewrites don’t seem to fire, confirm mod_rewrite is enabled and AllowOverride is set so .htaccess is read.

More detail: PHP header() docs and Apache mod_rewrite docs are good references: PHP header() and mod_rewrite overview.

Recommended Answers

All 2 Replies

You have to send the 301 redirect code explicitly as a flag
These flags go in brackets after your RewriteRule. In your case you could try this:

RewriteRule ^(.*)_(.*)_(.*)\.html$ result.php?var1=$1&var2=$2&var3=$3 [R=301, L]

R=301 says "send the 301 permanent redirect"
the L flag says that this is the last flag for this command.

Here is a random googled page for RewriteRule flags:
http://www.htaccesselite.com/mod-rewrite-flags-vt101.html

Good luck!

I had a similar doubt too. Thanks for the link guyinpv.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.