0

I'm trying to perform an internal rewrite of example.com/abc to example.com/?//abc where 'abc' can by any string.

Tried the following rule:

RewriteEngine On
RewriteRule ^(\w+)/?$  /?//$1 [L,QSA,NC,PT]

However this is not redirecting as expected. From the logs, I can see the following entries:

applying pattern '^(\\w+)/?$' to uri 'abc
rewrite 'abc' -> '/?//abc'
split uri=/?//abc -> uri=/, args=//abc
internal redirect with / [INTERNAL REDIRECT]
applying pattern '^(\\w+)/?$' to uri ''

Not sure, why it's splitting the pattern to uri and arg based on '?'. Can any one help

  • I've now managed to redirect the url using the following rule: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{QUERY_STRING} "^$" RewriteBase / RewriteRule ^(.*)$ /index.html?//$1/ [L,R] This is now redirecting to the desired URL, but the url is changing in browser. Any ideas how to maintain the same URL – mintu mohan Aug 26 '19 at 10:47
  • Can anyone shed some light, please? – mintu mohan Aug 27 '19 at 13:00

1 Answers1

0

Since your second example works, remove the R flag. It sends the redirect to the browser.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} "^$"
RewriteRule ^(.*)$ index.html?//$1/ [L]
Anonymous
  • 11,748
  • 6
  • 35
  • 57
  • Thanks for your reply. Tried removing R flag, but now the redirect don't work. Enabled rewrite logging for apache and following are noted: ++++++++ applying pattern '^(.*)$' to uri 'abc' rewrite 'abc' -> '/index.html?//abc' split uri=/index.html?//abc -> uri=/index.html, args=//abc trying to replace prefix /var/www/html/player with / internal redirect with /index.html [INTERNAL REDIRECT] strip per-dir prefix: /var/www/html/player/index.html -> index.html applying pattern '^(.*)$' to uri 'index.html' ++++ – mintu mohan Aug 30 '19 at 06:04
  • Ok, can you answer these questions? **1.** Where is `index.html`? Is it `/var/www/html/player/index.html`? **2.** Where is this `.htaccess`? Is it `/var/www/html/player/.htaccess`? – Anonymous Aug 30 '19 at 13:51
  • **1**. Yes, index.html is in /var/www/html/player folder **2**. Yes, .htaccess is in /var/www/html/player folder – mintu mohan Sep 09 '19 at 12:00
  • Sorry, @Anonymous Still this is not working. Now, the URL stays, but the page is not redirecting. Please see the logs: strip per-dir prefix: /var/www/html/player/abc -> abc applying pattern '^(.*)$' to uri 'abc' rewrite 'abc' -> 'index.html?//abc/' split uri=index.html?//abc/ -> uri=index.html, args=//abc/ add per-dir prefix: index.html -> /var/www/html/player/index.html strip document_root prefix: /var/www/html/player/index.html -> /index.html internal redirect with /index.html [INTERNAL REDIRECT] strip per-dir prefix:/var/www/html/player/index.html->index.html – mintu mohan Sep 12 '19 at 09:56
  • @mintumohan Do you have anything else in your .htaccess? It looks like a conflict – Anonymous Sep 12 '19 at 22:01
  • Nothing else. Only the htaccess code you've mentioned – mintu mohan Sep 13 '19 at 04:22
  • @mintumohan What do you mean when you say "the URL stays, but the page is not redirecting"? It’s an internal rewrite, so you won’t see a redirect in the browser – Anonymous Sep 14 '19 at 02:05
  • What I need is the URL should remain same after the redirection. That is after the redirection, URL should be example.com/abc. – mintu mohan Sep 17 '19 at 12:08
  • Sorry, but I won’t be able to figure this out without looking at the server. The .htaccess should be working, so it could be a problem with the index.php file or something else. Good luck figuring it out. – Anonymous Sep 17 '19 at 15:35