Add/prepend a GET variable name using redirects in htaccess (to fix broken URL) - apache

I have a php script that relies on a $_GET variable called "serial." Normally the URL would look like this:
http://www.example.com/script.php?serial=XXXXXX
However, the person making the QR codes thought that the URL was too long and has already finished making QR codes that point to this URL:
http://www.example.com/script.php?XXXXXX
The part they left out was the "serial=", so PHP can't $_GET the variable anymore.
Is it possible to redirect using .htaccess to add the "serial=" back in?
Any help would be appreciated.
Thanks!

Try:
RewriteEngine On
RewriteCond %{QUERY_STRING} !^serial=
RewriteRule ^script\.php$ /script.php?serial=%{QUERY_STRING} [L]
If you want to redirect the browser, add a R to the inside of the square brackets so that it looks like [L,R].

Related

Redirecting a URL that contains a specific part to another URL using htaccess

I would like to know if there is a way to redirect a URL that looks like this:
https://www.example.com/p=d3d82c7c
to
https://www.example.com/polls/poll?poll=d3d82c7c
using htaccess.
What I want to do is to create a more simple link for the users. Is that possible with htaccess only?
PS: the code at the end may change within each URL.
Thank you very much.
With your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteRule ^.*=(.*) polls/poll?poll=$1 [L]
OR with your shown samples if your URI always starts with p then you could try following, but make sure either previous OR this only one of them should be used at a time.
RewriteEngine ON
RewriteRule ^p.*=(.*) polls/poll?poll=$1 [L]
You can use this method:
Redirect 301 /en/php/project.html http://www.example.org/newpage.html

Multiple conditions in htaccess

I have a htaccess file which grabs pretty much anything after the / sign and gives it to my index.php?i. This works perfectly but I want to take it one step closer and have the same htaccess use conditions.
Currently this is the navigation method mysite.com/stuff/cat
the htaccess simply grabs the string stuff/cat and my PHP can then load up contents about it. But what I want to do is: when the navigation is mysite.com/stuff/cat/images I want the htaccess to redirect to another file rather than my index.php. Is it possible to add some kind of If statement or condition in htaccess? (if string contains 'images' redirect to gallery.php?i=data)
here is my current code:
RewriteEngine On
RewriteOptions inherit
RewriteRule ^([a-zA-Z0-9/_\s'-()]+)$ index.php?i=$1
You can try the following (place it under your RewriteOptions:
RewriteRule ^([a-zA-Z0-9/_\s'-()]+)/images$ gallery.php?i=$1 [L]
I would recommend using the L flag (as above) in your other rule too.

.htaccess rewrite path without changing visible URL

I have a link which looks like that:
www.domain.com/file.php
I want to create .htaccess entry so that if someone goes to
www.domain.com/folder/name
they would be redirected to
www.domain.com/file.php
I've created .htaccess entry which works and it looks like this:
RewriteRule ^folder/name$ /file.php [L,R=301,L]
The only problem I have is that in browser address field user sees
www.domain.com/file.php
where as I want him to see
www.domain.com/folder/name
Is it possible to do this and if so, how do I do that?
You don't need the redirect, that's causing it to reload the page which causes the new URL to appear in the address field. You just want to tell the server what directory path is represented by the "friendly" URL. Try this:
RewriteRule ^folder/name$ /file.php [L]

Need to create a RewriteRule in .htaccess for a Magento website

I am currently trying to put a RewriteRule into my .htaccess file for my Magento website that will allow me to write a category URL in the following way:
http://mydomain.com/dir/<direction>/order/<order>/<Magento category URL path>.html
What I am basically looking to do is use my robots.txt file to make some of the category URLs not appear (specifically when you apply a different sort order to the category).
So let's assume I have the following URL:
http://mydomain.com/dir/asc/order/sales_index/footwear/mens-work-boots/motorcycle-boots.html
I would like that to be rendered just as if it the URL was:
http://mydomain.com/footwear/mens-work-boots/motorcycle-boots.html?dir=asc&order=sales_index
The code I have put in my .htaccess file is as follows:
RewriteRule ^dir/(.*?)/order/(.*?)/(.*?)$ $3.html?dir=$1&order=$2
For some reason, when I have this in there, I get a 404 error. Can someone point me in the right direction to make this work for me?
I tried with this on my server
RewriteRule ^(.*)/dir/(.*?)/order/(.*?)/(.*?)$ $4.html?dir=$2&order=$3 [R,L]
and when i issue request
http://myserver/dir/asc/order/sales_index/footwear/mens-work-boots/motorcycle-boots.html
i get proper redirection to
http://yuave.dev:81/footwear/mens-work-boots/motorcycle-boots.html.html?dir=asc&order=sales_index
May be you are missing [L] flag on your request.

Using mod_rewrite to put words in the URL instead of category IDs

I'm working on a Business Directory Website. What I'd like to do is transform the URL String of :
http://www.website.co.uk/business/results.php?category_id=11
To http://www.website.co.uk/business/Financial & Legal
How do I achieve this? Not too sure on the Rewrite Rules etc I am to use.
Thanks for any help, in advance.
I am now trying jut a test on a local host.
The HTML Code is as follows :
<h1>This is the PHP file.</h1>
Link Here
So all I want is the URL to be http://localhost/mod_rewrite/index.php/category
rather than : http://localhost/mod_rewrite/index.php?url=category
The .htaccess file is as follows (But doesnt appear to work)??
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ /index.php?url=$1 [L]
Well,
in general you don't use ampersands in URLs.
They will be replaced by "%26" when you try to call them as a link.
But a general approach towards your question could look like that:
RewriteEngine on
RewriteRule ^business/Financial.*?Legal$ results.php?category_id=11 [L]