0

How can I rewrite URL (i.e. remove last / after test) using .htaccess on php page from

from www.example.com/test/?sku=23456&qty=3 to www.example.com/test?sku=23456&qty=3 from www.example.com/page2/?page=3&emp=543 to www.example.com/page2?page=3&emp=543 from www.example.com/stream/?start=4&id=tdfcs45s&q=sat to www.example.com/stream?start=4&id=tdfcs45s&q=sat

I tried but it doesn't work

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
  • Presumably this should be an external "redirect", not an internal "rewrite"? Basically, you are just [removing the trailing slash](https://stackoverflow.com/questions/21417263/htaccess-add-remove-trailing-slash-from-url) from the URL-path. Is it just from this specific URL? Or any URL-path? Is `/test` a physical subdirectory? The rule you've posted doesn't appear to have anything to do with what you are trying to do? (The rule you've posted appends a `.php` extension.) – MrWhite Oct 18 '22 at 16:53
  • @MrWhite, yes, there are more pages like that and need to remove the `/` from the trailing URL for each page. And each page will have different set of query parameters. – user20268089 Oct 18 '22 at 22:23
  • @user20268089, Thanks for letting know. Could you please do add few samples of links examples in your question just append them to your question and that will help us to understand your question in better manner. Add them like: **from** which URL **to** which url you want them to redirect, thank you. – RavinderSingh13 Oct 19 '22 at 00:43
  • @RavinderSingh13, I have updated the question to include more links. – user20268089 Oct 20 '22 at 01:27
  • @user20268089, Thanks for update but your **from** and **to** urls are looking same, could you please do update them and let me know then. – RavinderSingh13 Oct 20 '22 at 02:55
  • @RavinderSingh13 Except for the trailing slash that is omitted. This looks like a standard "remove trailing slash" question? – MrWhite Oct 21 '22 at 00:43
  • @MrWhite, Hey buddy, Thanks for asking. In my humble opinion, it looks rewrite question(url-rewrite in backend one), where removing slashes could be part of it. But still waiting for OP to confirm few things as we see in previous comments but IMHO doesn't look like exact dupe to me, cheers. – RavinderSingh13 Oct 21 '22 at 00:46
  • @MrWhite Thank you, we will rewrite the url in backend. Since we have more than 20 pages and 300+ combination of querystring, so was thing if there is any way we can do it in htaccess. RavinderSingh13 Thank you for the code, but this is good when we have one or 2 pages and fixed query string. BTW, who is OP? – user20268089 Oct 21 '22 at 01:16
  • @user20268089, OP means Original poster of question in this case its you, cheers :) – RavinderSingh13 Oct 21 '22 at 01:20
  • @user20268089, As requested above could you please do mention from clear samples of **from** which url **to** which url you want to redirect/rewrite then we can figure it out that what's going on, add details in text format in your question please. – RavinderSingh13 Oct 21 '22 at 01:24
  • @RavinderSingh13 Thank you! I have updated the question. We just need to remove `/` before `?` in each URL. – user20268089 Oct 21 '22 at 04:31
  • As asked in my first comment, do these URLs map to physical directories? eg. Is `/test/` a directory on the filesystem? Or this an entirely "virtual" URL? If it's a virtual URL then you presumably have other directives already in your `.htaccess` file? Have you tried the solutions in the linked question above? If they don't work then what happens? What URL are you linking to internally? Presumably this should be a "redirect", not a "rewrite"? – MrWhite Oct 21 '22 at 17:47

1 Answers1

1

With your shown samples and attempts please try following .htaccess rules file. We need to use THE_REQUEST variable here of apache. Check this if this helps you, also clear your browser cache before testing your URLs.

RewriteEngine ON
RewriteCond %{THE_REQUEST} \s(/test)/(\?sku=(\d+)&qty=\d+)\s [NC]
RewriteRule ^ %1%2 [L]
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93