htaccess prepend path only if url matches - .htaccess

This is what I am trying to do:
The old path for a specific category:
`https://mywebsite.com/tag/something/somethingelse`
The new path only if the request is for https://mywebsite.com/tag
https://mywebsite.com/options/tag/something/somethingelse
This is only if the requested url has /tag and does not have /options/tag.
If the request is for protocol/domainname/tag without /options/ preceding /tag then we need to prepend it there.
As an example,need to redirect from this:
https://myseniorchoices.com/tags/facilityamenitiesskillednursing/tv-room
to this:
https://myseniorchoices.com/options/tag/facilityamenitiesskillednursing/tv-room
The "facilityamenitiesskillednursing/tv-room" is only an example, there are thousands of possibilities so it needs to be dynamic past the tag/
Please note also the plural "tags" turns into singular "tag", should that make a difference.

To go from your first provided link to your second use a RewriteRule:
RewriteRule ^tag/something/somethingelse$ /options/tag/something/somethingelse?&%{QUERY_STRING}

Turns out this was pretty straight forward, should have thought of it earlier. This worked for me:
RedirectMatch permanent tags(.*) https://myseniorchoices.com/options/tag$1

Related

.htaccess rewrite url that has already been changed

I am upgrading my site which involves new scripts and a different URL
structure. There are currently a few thousand pages so I want to
basically move them in to a subdirectory so that they are not lost.
I am not very confident with htaccess so can someone please confirm that
the first part I have done is correct:
Old URL: http://www.example.com/article/another-dir/page-group/whatever.html
RewriteRule ^article/?$ http://www.example.com/archive/ [R=301,NC,L]
To achieve this: http://www.example.com/archive/another-dir/page-group/whatever.html
Search engines will see the above as a permanent move and in the address bar
it will show the new url. Is this right?
The second part is on the new script - the url's could be improved but I am
unable to change all the script so thought about using htaccess again but am
not sure if it can be achieved like this.
At the moment the url looks like this:
url: http://www.example.com/category/4/categoryname
In the htaccess the current rewrite rule for this type of url looks like this:
RewriteRule ^category/(.*)/(.*)$ category.php?id=$1&slug=$2
Is it possible to change this so that in the url address bar I end up
with this:
http://www.example.com/categoryname
Basically I don't want to see either the number or category/ in the resulting
url as it will be easier for visitors to use - is that possible??
Thanks in advance for any help.
The second question related to passing in URI components for querystring redirect and then hiding those components in the URL I don't think would be easy, if even possible, using RewriteRules.
For the first question though. Given the sample URLs you mentioned, the RewriteRule would need to include capture and backreference if you want to preserve the full URL in the redirection. For example:
RewriteRule ^article/?(.*)$ http://www.example.com/archive/$1 [R=301,NC,L]

htaccess redirecting old urls

I'm working on my htaccess and I finally got a result I can deal with. First I've got this
RewriteEngine on
RewriteBase /
RewriteRule ^blog/([a-zA-Z0-9\-/\.]+)/?$ blog.php?id=$1article_title=$2 [L]
This helps show my urls like so:
https://www.mydomain.com/blog.php?id=10
To
https://www.mydomain.com/blog/10/title.com
https://www.mydomain.com/blog/10/
https://www.mydomain.com/blog/10
https://www.mydomain.com/blog/10/title.com/
Four things here...
First is that all of these are accessible. I only want the first to appear and the others to direct there.
Second is that the article title will have spaces. Is there a way to turn spaces into - rather than have spaces?
Third is...is it okay to have .com within the url at the end? It just happens to be part of a post article title.
Fourth is do I include the extra part of the url$article_title in the urls I use? I'm lost on that part. Is it the same as I did with the id?
Last I was curious to know if I needed the id to show up. Can I exclude that in some way?
If I have urls already in place that use blog.php?id=10 do I change that to the new one? If I can't find it I'd like the url to go to the first rewritten url. All of the possible examples should go to that first url. or one without the id.

htaccess rewrite URL with Google tracking code

First of all, sorry for my english... I hope you're going to understand my problem :)
My company has made an emailing campaign which included a link to our website. This URL has a Google tracking code to follow the campaign.
Unfortunately, the URL is wrong :
http://www.mysite.fr/category/mypage.html/?utm_source=source&utm_medium=medium&utm_campaign=campaign
but there's a slash between "mypage.html" and the tracking code which points to an inexistant page.
I tried these rules to redirect the url without sending a new email to everyone but they don't work :
RewriteRule ^/category/mypage\.html/\?utm_source\=source\&utm_medium\=medium\&utm_capaign\=campaign$ http://www.mysite.fr/category/mypage.html?utm_source=source&utm_medium=medium&utm_campaign=campaign [R=301,L]
Or :
RedirectPermanent /category/mypage.html/ http://www.mysite.fr/category/mypage.html?utm_source=source&utm_medium=medium&utm_campaign=campaign
I don't know how to write the rule. Can someone help me please ?
A few things are wrong with your RewriteRule. If you're putting this in an .htaccess file, you need to remove the leading slash (the prefix) in your regular expression because apache removes it from the URI before putting it through rules in .htaccess files. Second, you can't match against the query string (the stuff starting from the ?) in the regular expression in a RewriteRule.
Not sure why your RedirectPermanent isn't working. That looks fine other than the fact that the query strings automatically get appended if you leave them out. I think that's what you want, so remove the query string in the target:
RedirectMatch 301 ^/category/mypage.html/$ http://www.mysite.fr/category/mypage.html

Is it possible to handle such URL

http://www.example.com/http://www.test.com
I have tried many different methods using .htaccess with no luck. I need to get that second url coming as parameter. Is it possible to redirect it to index.php and get it as $_SERVER["REQUEST_URI"] or other method? Thanks
UPD: Looks like it is impossible to get whole URL, question marks are not recognized. Ideal example:
127.0.0.1/http://www.test.com/script.php?a=hello&b=world#blabla;par2?%par3
and i need to get in my index.php exact string
www.test.com/script.php?a=hello&b=world#blabla;par2?%par3
It's definitely possible: http://downforeveryoneorjustme.com/http://www.google.com/
As to how, it's been covered on ServerFault already
The Problem:
This is a problem with Apache running on Windows. Apache on Windows does not let you have a colon (:) in your REQUEST URI. This is basically for avoiding URLs like http://www.mysite.com/C:/SomeFile.exe but is actually annoying.
If you use mod_rewrite at the same time it will be skipped.
You and some applications (like wikipedia) uses colon : in URL. so what to do in Apache on Windows?
The Solution:
At the time of writing this answer this bug still persists and there is no absolute solution, BUT there is a trick:
You may change your URL to something like this:
http://www.mysite.com/url/http://www.test.com
in this example http://www.mysite.com/ is your SCRIPT PATH and /url/http://www.test.com is your REQUEST URI.
The problem will be gone if there is a Slash (/) before Colon (:).
You can get the URI but only without the fragment since that is not transmitted to the server. Try this rule:
RewriteRule ^http:/ index.php [L]
Then the requested URI path plus query (so the part from the third / up to the first # or the end of the URI) is available at $_SERVER['REQUEST_URI'].

How to use htaccess to rewrite url to html anchor tag (#)

I have a situation where I want to take the following URL:
/1/john
and have it redirect using Apache's htaccess file to go to
/page.php?id=1&name=john#john
so that it goes to an html anchor with the name of john.
I've found a lot of reference to escaping special characters, and to adding the [NE] flag so that the redirect ignores the # sign, but these don't work. For example, adding [NE,R] means that the URL just appears in the browser address as the original: http://example.com/page.php?id=1&name=john#john.
This is possible using [NE] flag (noescape).
By default, special characters, such as & and ?, for example, will be converted to their hexcode equivalent. Using the [NE] flag prevents that from happening.
More info http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_ne
You can in fact do one of these things, but not both.
You can use the [NE] flag to signify to Apache not to escape the '#' character, but for the redirect to work, you have to specify an absolute URL to redirect to, not simply a relative page. Apache cannot do the scrolling of the window down to the anchor for you. But the browser will, if you redirect to an absolute URL.
What you want to do, can be accomplished with URL rewriting, or, more specifically, URL beautification.
I just quickly found this well explained blog post for you, I hope it can help you out with the learning to rewrite URLs-part.
As for the #-thing (expecting that you now know what I'm talking about), I don't see a problem in passing the same variable to the rewritten URL twice. Like: (notice the last part of the first line)
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)$ /$1/$2/#$2 [R]
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/$ /index.php?page=$1&subpage=$2
Though, you'll have to escape the #-part, and it seems that it can be done this way:
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)$ /$1/$2/\%23$2 [R,NE]
BTW, URL rewriting is not that hard (but can become complicated, and I'm not an expert), but Google can help a lot along the way.
You cannot do an internal redirect to an anchor. (Just think about it: how would Apache scroll down to the anchor?) Your link should pointo to /1/john#john. Anchors aren't part of the request uri.

Resources