.htaccess redirect based on url value - .htaccess

I have a url as below.
http://example.com/listing?listingid=237508&2000
What I want is to redirect it to a mobile version of the site.
http://example.com/mobile-listing?listingid=237508&2000
The problem is that if it is example.com/listing it should redirect to example.com/mobile-listing. If it is example.com/event it should redirect to example.com/mobile-event and so on. How am I to do this? I have seen question about redirection to mobile version based on url parameters or getvalue. But not this way. Any help would be greatly appreciated. Thanks.

i've found a simuluar answer from ulrich palha which might helps.
question id 9113747
in the .htaccess file of root you can try this:
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)listingid= [NC]
RewriteRule ^ mobile-listing [L,R=301]
hopes this helps. you might change a little and this should point in the right direction to the solution.

You need to use a RewriteRule directive and QSA and PT flags
<VirtualHost *:80>
RewriteEngine on
RewriteRule ^/listing$ /mobile-listing [PT,QSA]
…
</VirtualHost>
Flags
PT:
Forces the resulting URI to be passed back to the URL mapping engine
for processing of other URI-to-filename translators, such as Alias or
Redirect. details ...
QSA:
Appends any query string from the original request URL to any query
string created in the rewrite target.details
More information
You can find more scenarios at Redirecting and Remapping with mod_rewrite.

Without any sort of condition for what is mobile or what isn't, and you always redirect, then the rule would look something like this:
RewriteCond %{REQUEST_URI} !^/mobile-
RewriteRule ^([^/]+)$ /mobile-$1 [L,R]
The query string will automatically get appended. Change the R to R=301 if you want to permanently redirect.

Related

Redirect index.php with parameters to a folder and remove parameters using htaccess

I have searched but cannot find a specific answer for this exact redirect style...
I have this structure of URL with this specific parameter:
https://websitename.com/directory/index.php?option=com_virtuemart&view=cart
I want it redirected to:
https://websitename.com/shopping-cart/
Note that the above mentioned "directory" changes, but the index.php with the parameters stay the same. No matter what the directory is, I always want it to go to the same exact redirect.
I cannot seem to get the right redirect working in htaccess. Can anyone help?
You can use this redirect rule as your first rule in site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} /(?:index\.php)?\?option=com_virtuemart&view=cart [NC]
RewriteRule ^ /shopping-cart/? [L,R=308]
# remaining rules go below this
You can use a set like this. It takes care on the param view=cart
RewriteCond %{QUERY_STRING} (^|&)view=cart
RewriteRule ^(.*)$ /shopping-cart/? [L,NC,R=301]
If you want to keep the querystring params, then change
/shopping-cart/?
to
/shopping-cart/
without questionmark

What to do with Question Marks in HTAccess Redirects

I'm new to redirects and have researched how to do this but am just getting more and more confused.
What I'm wanting to do is create a rule to redirect:
From: http://www.example.com/wordpress/?p=1250
To: http://www.otherexample.com/blog
Where I'm running into issues is with the question mark. I've read somewhere that I'm not sure I'm doing this correctly, but here's what I have so far:
**Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^p=1297$
RewriteRule ^wordpress$ linktoothersite [R=301, QSA, L, NC]**
I'm failing somewhere, any of you see what I'm doing wrong? Any help would be appreciated. I could only post two links, so link to other site would be the link.
If it's a single link you want to change you don't need a htaccess rule for that you can just do a redirect.
redirect 301 /wordpress/?p=250 http://www.othersite.com/blog
If the URL parameter changes you can use this rule.
RewriteEngine on
RewriteRule ^wordpress/(.*) http://www.othersite.com/blog [R=301,QSA,NC,L]
Note that when you use the QSA flag it will append the URL with the query parameter so if that's what you want the resulting URL will be.
http://www.othersite.com/blog?p=250
Otherwise if you remove the QSA flag, then it will just redirect to this
http://www.othersite.com/blog

Please help to rewrite a URL

Sorry Please i am a new bie in url rewriting.
I have a url like this
http://www.sitename.com/subpages/detail.php?id=21-0043-052&car_name=abc
i want to redirect this site to
http://www.qualitextrading.com/vehicle-detail.php?id=21-0043-052&car_name=abc
Please how i can write this in .htaccess file
Thanks in advance
Assuming you have mod_rewrite enabled:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.sitename.com$ [NC]
RewriteRule ^\/subpages\/detail\.php(*.?)$ http://www.qualitextrading.com/vehicle-detail.php$1 [R=301,L]
This turns on rewriting for this area, then checks if the HTTP_HOST is correct, then rewrites the URL and passes on the query string (default behavior of mod_rewrite passes on query string automatically).

.htaccess redirecting secure

Haven't found what i'm looking for so far.
I want to redirect all my site visitors to the secure version of the URL they type in, so i've put this in my .htaccess-file.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R,L]
So far so good, but now I want that visitors who type in
www.mywebsite.com/directory/param1/etcetera/ or
www.mywebsite.com/directory/file.php?param1=value1
are being redirected to
https://www.mywebsite.com/directory/param1/etcetera/ or
https://www.mywebsite.com/directory/file.php?param1=value1
as well, cause that isn't happening.
Can some guru help me out? :) thnx
Your first case, www.mywebsite.com/directory/param1/etcetera/, should be redirected to its https variant by your RewriteRule. I can't see why that wouldn't work.
To cover the second case, in order to have Apache include the query string, you need to add the QSA flag, so:
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R,L,QSA]

How to remove part of a URL using .htaccess

I have a url like this.
/domains/details.php (NOTE: domains is dynamic and can be anything)
How do I remove the domains part from the URL using .htaccess so the actual lookup is:
/details.php
OR it'll be cool if I can get domains into the URL.
/details.php?page=domains
Thanks!
Scott
RewriteEngine on
RewriteBase /
RewriteRule ^([^/]+)/details.php$ /details.php?page=$1 [R=301]
Leave off the [R=301] if you want an internal redirect rather than an actual HTTP redirect.
To preserve existing query parameters you can change the rule to this:
RewriteRule ^([^/]+)/details.php(.*)$ /details.php?page=$1&%{QUERY_STRING} [R=301]
Please try to use the following rules to deal with your last request:
RewriteRule ^(?!domains/.*)([^/]+)/details.php$ domains/details.php?page=$1 [R=301,QSA]
RewriteRule ^domains/details.php$ details.php [NC,L]

Resources