Is it possible with .htaccess to check if a URL has a certain path and then reconstruct it?
My issue is that I need to take a URL in this format:
https://example.com/subdirectory/group/test_1
and if the URL matches:
https://example.com/subdirectory/group/
without the hash to reconstruct the URL adding the hash to be:
https://example.com/subdirectory/#/group/test_1
so in essence I would be checking if the URL is:
https://example.com/subdirectory/group/
and if it is changing it to be:
https://example.com/subdirectory/#/group/
RewriteRule ^subdirectory/(group/.*) /subdirectory/#/$1 [R=302,NE]
That rule should basically do the trick.
R=302 for testing, make that into a 301 once you verified it works properly.
The NE / noescape flag is necessary, so that the # stays a #, and doesn't get escaped to %23.
Related
I have changed the path of some of my urls, and I'd like to redirect them to some new urls with an extra query string ?industry=
Old url: e.g. https://domain.ext/skills/keywords/list.php?q=account+manager
https://domain.ext/skills/keywords/list.php?q=assistant
New url: e.g.
https://domain.ext/skills/keywords/list.php?industry=human-resources&q=account+manager
https://domain.ext/skills/keywords/list.php?industry=banking&q=account+manager
https://domain.ext/skills/keywords/list.php?industry=banking&q=assistant
As you can see the new URls contains a new query parameter industry= which can have different industries.
So how can I check if a new url exist with the query ?q=account+manager for example and then redirect the old url to one of the new url that match &q=account+manager?
I've looked into RewriteRule with .htaccess, but I haven't been able to find the right redirection yet. If someone could share some thoughts. Thanks.
Not sure what you wish to do, but I will take this question as a request for a simple example of a Rewrite Rule. Depending on what you are trying to achieve, several approaches may be used.
The code in the .htaccess file could be like this:
# essential
Options +FollowSymLinks
RewriteEngine On
# Here a simple rule, the link ending with assitant will be sent to list.php as the q parameter
RewriteRule ^skills/keywords/assistant$ list.php?q=assistant
# You may use variables and Regex, such as $1, $2, $3 in this case to receive the values
# in the parentheses for the three parameters
RewriteRule ^(en/fr/es)/(keywords)/([\/\.\w_-]{3,})$ list.php?lang=$1§ion=$2&q=$3 [L,R=301,NC]
Flags are these ones:
NC = No Case
R=301 = returns a 301 (permanent redirection) code rather than a
302 (temporary) for SEO purposes
L = Last ruel to be executed,
next ones are ignored.
I haven't check if this code works, when testing this on your server, you will encounter a blank page if something is wrong. Also, if you use flags, make sure there is no whitespace after the comma.
This page explains all this very well: https://httpd.apache.org/docs/trunk/en/rewrite/intro.html
I'm trying to find a redirect that will remove part of a URL (in the middle), but leaves the query string in place at the end.
I can do this fine via a single url redirect, but there are hundreds of these urls so I'm trying to find a rule that might be able to do for all of them in one fell swoop, so i don't have to make one for each and any new ones will get redirected automatically.
I'm trying to remove 'search.php' from the urls, here is an example:
www.site.com/products/search.php?rPage=/items/listing/detail_handler.jsp?code=219592&units=Feet&item_id=2624472
to redirect to:
www.site.com/products/?rPage=/items/listing/detail_handler.jsp?code=219592&units=Feet&item_id=2624472
Thanks for your time.
According to the documentation, the query string is passed through unchanged by default
RewriteRule products/search.php http://www.site.com/products/ [L,R=301]
I have a rule in my .htaccess file to overwrite ugly parameter urls with clean paths.
Here is an example:
RewriteRule ^landing(/)?([0-9]+)?(/)?$ landing/index.php?intPage=$2
So when someone goes to /landing/3, it would load the index.php page passing intPage parameters as 3.
Very simple.
However, I'm using techniques to trace Google Analytics referals, so I need to tack parameters onto this URL:
For example:
/landing/3?kt_type=ad&kt_st1=adwords&kt_st2=prize_a_us.en
The problem is that, I don't know how to retrieve the parameters that are tacked onto the URL, since the URL has already been overwritten and now I can't retrieve kt_type, kt_st1 etc.
Any idea on how to make it possible so I can still tack parameters to already overwritten URLs?
Use QSA flag. Change your rule to this:
RewriteRule ^landing/?([0-9]+)?/?$ landing/index.php?intPage=$1 [L,NC,QSA]
From official docs:
QSA - Appends any query string from the original request URL to any
query string created in the rewrite target
Stupidly, I have sent out a newsletter without checking the links. One of which is broken so I want to handle this with htaccess.
My URL that is being linked to is:
http://www.domain.com.au/www.domain.com.au/campers-and-motorhomes/ne%20w-zealand/camper-rentals/
where the actual page is:
http://www.domain.com.au/campers-and-motorhomes/new-zealand/camper-rentals/
Note the space in new zealand as well as the additional www.domain.com.au
How can I set this up in htaccess?
Thanks
Since you don't have to manipulate the URL, you can use a simple Redirect:
Redirect /www.domain.com.au/campers-and-motorhomes/ne%20w-zealand/camper-rentals/ http://www.domain.com.au/campers-and-motorhomes/new-zealand/camper-rentals/
Edit If Apache doesn't like the space unquoted as %20, try quoting the whole thing with a real space in there:
Redirect "/www.domain.com.au/campers-and-motorhomes/ne w-zealand/camper-rentals/" http://www.domain.com.au/campers-and-motorhomes/new-zealand/camper-rentals/
Edit2 If it's appending a query string, you will need to use mod_rewrite to get rid of the querystring rather than a simple redirect, I'm afraid.
RewriteEngine On
# If the request starts with www.domain.com.au, it is the broken link
# Rewrite to the proper URL and put ? on the end to remove the query string
RewriteRule ^www\.domain\.com\.au http://www.domain.com.au/campers-and-motorhomes/new-zealand/camper-rentals/? [L,R=301]
I am moving a website to a new Open Cart install and I have set up 301 redirects to keep any SEO value.
I had to use the following trick here - http://www.itsadam.co.uk/opencart-301-redirect-not-working-seo-fix-route/ - to work around an annoying issue with the way Open Cart handles url rewrites. So, I have this sort of thing in my .htaccess file currently:
redirect 301 /products-page /products?
However, this now leaves my rewritten urls with a trailing ? - an empty query string effectively:
http://www.mysite.com/products?
Is there a way I can use rewrites to match and ditch any superfluous question marks? (I need to retain any actual query strings).
I encountered the same issue in opencart. Rather than follow the link to your solution do it like this:
RewriteRule ^original-url$ new-url [L,R=301]