0

I need help with using RewriteCond properly in my .htaccess. I have a simple php file which accepts 2 get parameters and returns a JSON object. Well, I wrote a simple Rewrite Rule to make my url pretty and understandable. Although after adding a form, obviously the Rewrite Rule won't get triggered because it will send a standard 'raw' URL.

My normal URL looks like this:

www.example.com/?ost=1&song=2

After my Rewrite Rule:

www.example.com/ost/1/song/2

But the form sends the url like this: www.ost.com/?ost=1&song=2

After searching a bit, I found out that RewriteCond could help but I am not familiar with it.

Edit: I have been asked for my Rule, so here it is:

RewriteRule ^(ost)/([^/.]+)/?(song)?/?([^/.])?$ index.php?$1=$2&$3=$4

I forgot to mention that the song parameter is optional but this doesn't matter.

Dusto
  • 45
  • 10
  • Can you share the rule you are currently using in your htaccess? – Amit Verma Dec 18 '21 at 15:32
  • The form will always send using the query string format, unless you throw JavaScript at it. In htaccess, you _could_ throw a 30x redirect at query string URLs, but does it really matter? If you are worried about search, the bots don’t fill out forms, but you can also just include a canonical link tag, too. – Chris Haas Dec 18 '21 at 15:35
  • "But the form sends the url like this" - you need to change the form so that it also sends the request to the "pretty" URL. Once you implement the necessary rewrites in `.htaccess` to enable the "pretty" URLs then you should be using the "pretty" URLs _everywhere_ (the old "parameter" URLs should never be exposed to the end user). – MrWhite Dec 18 '21 at 17:54
  • @ChrisHaas - Well I wanted it to work like this: A form in my index.php sends data via Get parameter to my php.file and after receiving the JSON object I wanted the URL to look like described in my Question. – Dusto Dec 18 '21 at 19:23
  • @MrWhite - Sounds about right, but how would I do that? We cannot forget that at the end the URL has to look like the "pretty" URL. Regards – Dusto Dec 18 '21 at 19:24
  • I personally avoid redirects unless I’m trying to fix something, or have a very specific need. In this case, putting a redirect into htaccess for a new long-term feature doesn’t sound like a great idea to me. My recommendation would be to instead use JS to hook into the [form’s submit](https://stackoverflow.com/a/5640946/231316) and dynamically change the action to match your route. When using pretty URLs, you really don’t want any of the file extension stuff sneaky out there, even if it happens really fast. – Chris Haas Dec 18 '21 at 19:35
  • @ChrisHaas - Thank you, I will have a look at it. It seems like you are right and it is a better option. Let's try if I get that to work :o – Dusto Dec 18 '21 at 19:41

0 Answers0