special characters in url using htaccess - .htaccess

The idea is to use www.example.com/admin/#!/12345 as www.example.com/admin/user.php?id=12345
This is the rewrite rule Im trying, but this doesn't seem to work
RewriteRule ^admin/#!/(.*) www.example.com/admin/user.php?id=$2
How do I include such special characters ?
Thank you

You can't use # is URLs. The browser will interpret it as a fragment identifier and never even send it (or the following part of the URL) to the server.

Related

RewriteRule from download.php?token=something into /download/something

I am working on url, I try htaccess, php, javascript and many other things but unable to figure it out.
My url is :
example/cheap-flight-to.php?country=lagos
and I want to change the url something like this :
example/cheap-flight-to.lagos
or
example/cheap-flight-to/lagos
please help me
The following should allow you to generate your urls in the format that you wish.
RewriteEngine On
RewriteBase /
RewriteRule ^example/cheap-flight-to/([a-zA-Z]+)$ /example/cheap-flight-to.php?country=$1 [NC,L]
What you want could be done using regular expressions in .htaccess, but it makes no sence, since it wouldn't be pointing to anything, unless you have a directory cheap-flight-to/lago in which you have an index.php that will show-up in the browser or return data. This means you have to setup a directory for each destination you want to go to. Is that really what you want? Usually it's been used the otherway around. The user (or a script for that matter) enters a url like "example/cheap-flight-to/lagos". Then through regular expressions in .htaccess you rewrite the url to "example/cheap-flight-to.php?country=lagos".
Have a look at http://httpd.apache.org/docs/1.3/misc/rewriteguide.html and
http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html for more on rewriting rules in .htaccess.

.htaccess URL rewrite issue

I've run into a problem when trying to rewrite a url that has a # in it. The rewrite itself is simple:
RewriteRule ^accessories/access/Roll-Up-PR30/accessories.php#a485$ /alog/roll-up-product.php
But it just falls over and will not let me have the hash in there.
Any feedback is welcome as I am running out of ideas :S
Unfortunately most browsers (that I know of) don't send the fragment part of the URL to the server so it's not available in .htaccess; it's just a client-side anchor lookup.
It seems that hashes are a special case. See http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
The relevant section is "Extended Redirection"
RewriteRule ^xredirect:(.+) /path/to/nph-xredirect.cgi/$1 \
[T=application/x-httpd-cgi,L]

.htaccess url masking

I have a couple of wordpress urls that I want to simplify for SEO purposes. I have
http://www.example.com/fr/product-fr/review-fr/
I would like to change that to http://www.example.com/fr/product/review/ (remove -fr) but without changing the URL internally. So the user would access http://www.example.com/fr/product/review/ but the server would serve the content of http://www.example.com/fr/product-fr/review-fr/.
I want to do this to get around a URL problem with Wordpress and WPML.
Appreciate the help
Assuming the two letter lang code is always going to be constant throughout the URL:
RewriteEngine on
RewriteRule ^([A-Za-z]{2})/product/review$ /$1/product-$1/review-$1 [L,QSA]
should work a treat :)
P.S. the regexp takes any two-letter code (upper or lower case) so will work with other langs should you require.
Have you tried mod_rewrite and Rewrite rules?
Try:
^/([a-zA-Z]{2})/([^\/]+)/([^\/]+)/$ /$1/$2-$1/$3-$1
Haven't tested this though..

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