0

I am stuck with a mod rewriting on Apache htaccess.

my URL looks like this :

http://localhost/Ac5GRnXW

I want it to redirect like this:

http://localhost/redirect.php?id=Ac5GRnXW

I am using the following code :

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^(\w+)$ ./redirect.php?id=$1

Please help

Anjali
  • 1
  • The script you've posted should already do as you require (although you should remove the `./` prefix from the _substitution_ string). Have you enabled `.htaccess` overrides in the server config? (`.htaccess` files are an _optional extra_ - they are not processed by default.) – MrWhite Oct 31 '22 at 12:49
  • yes htaccess is configured in httpd conf file and removing ./ prefix wont help – Anjali Oct 31 '22 at 15:17
  • So what happens exactly? Are you getting an error? Undesirable response? Nothing? Where are the `.htaccess` and `redirect.php` files located? Do you have any other directives in the `.htaccess` file? How is your server configured? Try writing some "nonsense" at the top of the `.htaccess` file - do you get an error? – MrWhite Oct 31 '22 at 16:02
  • all files located in root directory. I am getting 404 error URL not found when trying to access http://localhost/Ac5GRnXW. I don't have any other directive in htaccess file. writing some nonsense at top of it gives 500 error for internal server. – Anjali Oct 31 '22 at 16:46
  • To clarify, requesting `/redirect.php?id=Ac5GRnXW` directly works as intended? Try (temporarily) changing the rewrite into an external redirect, ie. `RewriteRule ^(\w+)$ /redirect.php?id=$1 [R,L]` (note that the subst string starts with a slash in this instance). What happens? Are you redirected? – MrWhite Oct 31 '22 at 17:18
  • RewriteRule ^(\w+)$ http://www.google.com <- even this wont work – Anjali Nov 01 '22 at 00:25
  • Remove the two `RewriteCond` directives. – MrWhite Nov 01 '22 at 00:56
  • removing two conditions even wont work – Anjali Nov 01 '22 at 01:15
  • And you have no other directives in the `.htaccess` file? – MrWhite Nov 01 '22 at 01:25
  • yes I have no other directives in htaccess file – Anjali Nov 01 '22 at 02:04
  • Remove the existing rule and try the following: `RewriteRule ^foo$ /bar [R,L]` - Request `/foo`, are you redirected to `/bar`? – MrWhite Nov 01 '22 at 10:37

0 Answers0