Score:0

Want to redirect URL and change slug to url parameter

tj flag

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

kz flag
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.)
Anjali avatar
tj flag
yes htaccess is configured in httpd conf file and removing ./ prefix wont help
kz flag
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?
Anjali avatar
tj flag
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.
kz flag
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?
Anjali avatar
tj flag
RewriteRule ^(\w+)$ http://www.google.com <- even this wont work
kz flag
Remove the two `RewriteCond` directives.
Anjali avatar
tj flag
removing two conditions even wont work
kz flag
And you have no other directives in the `.htaccess` file?
Anjali avatar
tj flag
yes I have no other directives in htaccess file
kz flag
Remove the existing rule and try the following: `RewriteRule ^foo$ /bar [R,L]` - Request `/foo`, are you redirected to `/bar`?
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.