htaccess rewrite rule - url param - apache

This is probably incredibly simple, but I've gone through several links and can't find the answer that I'm looking for.
My URL:
www.website.org/shows/index.php?id=1
Desired Rewrite:
www.website.org/shows/id/index.php
I know I need to use the RewriteRule in .htaccess for Apache, I just can't figure out how to get it to match then rewrite.
My failed attempt:
RewriteRule ^/shows/index.php?id=([0-9])$ shows/$1/index.php

Your rewrite rule should be like that:
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^shows/index\.php$ /shows/%1/index.php
There is a very good official documentation, which explains how to access quesry sting in rewrite rules: https://simonecarletti.com/blog/2009/01/apache-query-string-redirects/

Related

Rewrite in HTACCESS

Can anyone help me here? I did a rewrite for the english version of my site.
It's was mysite.com/?lang=en And now it's mysite.com/en/
I've tried redirecting the URL with this :
RewriteRule ?lang=en$ /en/ [R=301,L]
But each time I put this line in my HTACCESS, the website goes down.
Your rewrite rule is trying to use the parameter portion of the URL. There is an existing SO answer with an example:
HTAccess Rewrite based upon a Parameter Value, ignore other Parameters
Perhaps something like this will help:
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)(lang=en+).+ [NC]
RewriteRule ^en$ %{REQUEST_URI}?%1 [R=302,NE,NC,L]

Rewrite Rule - need guidance

I need to do some redirecting to get some internal links to work but I'm having a complete block.
The url would be http://www.something.com/faqs/What_happens_if_I_move_home?
redirected to http://www.something.com/faqs/index/What_happens_if_I_move_home?
but it must look like the original url. I'm sure there is a simple answer but rewrite rules and regex are a mystery to me at times.
I did try RewriteRule ^faqs(/.*)?$ /faqs/index$1 [R,L,NC]
amongst many others!
try this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/faqs/index
RewriteRule ^faqs/(.*) /faqs/index/$1 [L,NC]

assistance to with rewrite rule

I want URL to look like www.example.com/chat/91 but really the page is www.example.com/room.php?id=91 I searched online for apache rewrite example but I didn't see any examples in this format to use,
Use this RewriteRule in your .htaccess file:
RewriteEngine On
RewriteRule ^chat/([^/]*)$ /room.php?id=$1 [L]
Make sure you clear your cache before testing this.

URL: transform "/news.php?id=1" to "/news/1/"

I'm trying to make some more cleaner URL's for my website. I have allready found lots of questions regarding the removal of file extensions in URL's and managed to get that working. I want to take it a step further though by transforming an URL like http://www.site.com/news.php?id=1 into http://www.site.com/news/1/. I couldn't find an answer to this specific question so I'm asking it here. How could I achieve URL's like that?
Rewrite rules I allready have in my htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ $1.php
There are plenty of questions out there that answer your question. There are two things you have to do:
Redirect the ugly url to the fancy url, so that the user sees the fancy url
Internally rewrite the fancy url to the ugly url, so that the server can actually execute it.
You can find the documentation for mod_rewrite here. This solution uses the [END] flag which is only available from Apache 2.3.9 and up. You can find the documentation here. The trailing ? in the first rule discards the query string.
#Redirect ugly url to fancy url
RewriteCond %{QUERY_STRING} id=([^&]*)
RewriteRule ^news\.php$ news/%1/? [R]
#Internally rewrite fancy url to a usable url
RewriteRule ^news/([^/]*)/?$ news.php?id=$1 [END]

Rewrite rule on multiple variables using Apache mod_rewrite

I would like to know how can I rewrite www.site.com?lang=lv&type=1&mode=2 into www.site.com/lv/1/2 using Apache mod_rewrite options.
Assuming that you actually want to rewrite /lv/1/2 to ?lang=lv&type=1&mode=2 (I see no reason to do the opposite) and that no other rewrites are active, this should do the trick:
RewriteRule ^/([^/]*)/([^/]*)/([^/]*)$ ?lang=$1&type=$2&mode=$3 [L]
Also; you'd be better off replacing those magic numbers with more useful information if you want to include them in your URI.
Edit: If it really is the opposite you'd like to do, see the answer by Matt S.
Basic rewrite:
RewriteEngine On
RewriteRule http://www.site.com/?lang=(.+?)&type=(\d+?)&mode=(\d+?) http://www.site.com/$1/$2/$3 [L,R=permanent]
Edit: Changed rewrite flags to force a permanent redirect
You need to handle the URI path and query separately. If the parameters appear in that very same order, you can do this:
RewriteCond %{QUERY_STRING} ^lang=([^&]+)&type=([^&]+)&mode=([^&]+)$
RewriteRule ^$ /%1/%2/%3? [L,R=301]
Otherwise you will need to put them in the right order before using this rule or take each parameter at a time.