0

At present the webpage accepts get params like this https://TripTale.in /details.php?id=AlphaNumericKeys
Which I want to make it look like https://TripTale.in /details/someTexts/AlphaNumericKeys

Contents of my .htaccess file

Options All -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?tripTale\.in)$ [NC]
RewriteRule .? https://%1%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? /404.php [L]

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^details/(.*)$ details.php?q=$1 [QSA]

RewriteRule ^details/(\d+)/([\w-]+)$    details.php?id=$1&title=$2

RewriteRule ^details/([0-9]+)/([A-Za-z0-9-\+]+)/?$ /details.php?id=$1&title=$2

All the above tried code does not works and shows error 404.
However the below code works but makes all the images file as blank.

Options All -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?tripTale\.in)$ [NC]
RewriteRule .? https://%1%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? /404.php [L]

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule .? /404.php

RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+) details.php?id=$1&title=$2 [QSA]
4
  • I recommend putting your database keys closer to the front of your URLs. Long URLs have a tendency to get truncated by emails and when displayed in other sites. When the db key is near the front, you can usually redirect to the full URL from a truncated URL. When the id is at the end, that isn't usually possible. Commented May 17, 2023 at 17:40
  • @StephenOstermiller "I recommend putting your database keys closer to the front of your URLs"- That "kind of" looks like what they have done in the last rule that supposedly "works"? However...
    – MrWhite
    Commented May 17, 2023 at 23:41
  • "the below code works" - Except that I can't see how that would "work" at all? It doesn't match the example URL you posted and the preceding rule(s) would first rewrite the request to /404.php so the final rule is never processed? Where have you put the .htaccess file?
    – MrWhite
    Commented May 17, 2023 at 23:45
  • "but makes all the images file as blank" - usually an indication you are (erroneously) using relative URL-paths to your images (and possibly other static assets). See: .htaccess rewrite URL leads to missing CSS
    – MrWhite
    Commented May 17, 2023 at 23:47

1 Answer 1

1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? /404.php [L]

Shouldn't those three lines be just one: ErrorDocument 404 /404.php

You may end up using that statement to direct to details.php instead


RewriteRule ^details/(.*)$ details.php?q=$1 [QSA]

details.php is looking for q? Not id as in details.php?id=AlphaNumericKeys?

I have been wanting to learn more about this technique and recently bookmarked a page that may be helpful to you.
[HOWTO] Rewrite all urls to one index.php in Apache


the below code works but makes all the images file as blank

RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+) details.php?id=$1&title=$2 [QSA]

That's because you are rewriting everything. details/someTexts/AlphaNumericKeys becomes details.php?id=details&title=someTexts/AlphaNumericKeys and images/graphic.png becomes details.php?id=images&title=graphic.png

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.