Problems with a simple URL rewrite (htaccess) - .htaccess

I can't get a simple htaccess rewrite to work.
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /?id=$1 [L]
I went to mysite.com/?id=blah expecting to end up on mysite.com/blah.html. What's wrong?

This is not how htaccess works. It doesn't re-write the URL visible to the user, rather it looks more like an invisible re-direct.
I'm not even sure what that rewrite in the question is supposed to be doing... BUT, for explanation purposes, let's say you wanted a user to be able to go to:
mysite.com/blah/
but you wanted the server to see it as:
mysite.com/index.php?page=blah
You could do this:
RewriteRule blah/ index.php?page=blah
The user would ALWAYS SEE WHAT THEY TYPED IN. It's not going to change the URL in the browser bar (that would be a header redirect or something, which is completely different and not really related).

Related

How to properly configure .htaccess (rewrite)

This is my first question here and all I can say is that I am really glad to be part of this community.
Now, as part of the question. I have a small problem configuring my htaccess properly, I have tried numerous lines of code which I have found here and there, trying my best to come close to something relevant or close to what I want, but no matter what.. I was failing. I tried redirect, redirect 301, both with and without full links, rewrite url with and without extension in the end but nothing happened.
So, what's my question exactly.
I have a form which when somebody clicks on the "send" button, the form tries to launch a file. It produces the following link:
domain.com/api/services/notification/send.php?accept=json&contentType=json&appUrl=http://domain.com
Although, while I have send.php under api/services/notification/ it's not called at all because there's no extension when the form produces the link, resulting in a 404 error. So, I tried several times to make .htaccess to redirect send into send.php but every time I was facing a huge fail. My assumption is; because it's within other letters/words. Mainly, I want .htaccess to "replace" send into send.php without affecting the rest of the URL, using mod_rewrite.
Any insights on this? The last code which I hoped that would work but didn't at all, was this one:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^send?([^/]+) /send.php?accept=json&contentType=json&appUrl=http://domain.com [NC]
You can try this rule:
RewriteEngine on
RewriteBase /
RewriteRule ^/api/services/notification/send$ /api/services/notification/send.php [L]

.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]

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.

How to redirect an erroneous URL

I just noticed that sometimes (even when given a wrong url) load perfectly fine. How do they accomplish this? What I mean is, suppose you click on a link that seems good like www.foo.com but it contains in the end a space character which would appear on the address bar as www.foo.com%20 some sites manage to redirect this to their correct url while others just break. How can this be achieved? I'm guessing it's something to do with the .htaccess but I have no idea what to do or where to do it.
The URL I'd like to redirect looks like this actually: http://foo.com/%C2%A0
I get the following error message:
The requested URL /%C2%A0 was not found on this server.
How can I make this redirection?
So far I came up with:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^%?\ ]*\%
RewriteCond %{REQUEST_URI} !^/
RewriteRule ^(.*)$ http://www.foo.com/ [R=301,L]
but it's not working at all
URL Rewrite would be the IIS version that may exist in other forms if you want to look at re-writing the URL assuming you mean this kind of case.
Don't forget that browsers may make certain guesses about what someone enters so that if someone types in "foo.com " that the browser may trim white space by default rather than URL encode the text. If "http://foo.com" fails then it may try "http://www.foo.com" for another idea as these could be seen as simple interpretations to take on what someone types in. If both fail then it may just Google the text believing that the address bar should be treated like a search box.

.htaccess redirect/rewrite help needed

I am writing htacess redirect rule but it is not working properly I have tried many solutions but simply it is not working.
What I want to do is to
I have url http://example.com/cms/index.php?page=filename I want this url to be executed and show appropriate page but in browser it should show example.com/cms. And what is important is I only want to right this rule for this page only and it should not effect to other pages.
Thank you.
RewriteRule ^([^/]+)/$ /cms/index.php?filename=$1 [L,QSA]
The L at the end says it is the last rule (stop processing) and QSA means 'Query String Append', so if someone puts other parameters after it, such as:
http://example.com/cms.htm?order=desc
The GET value for order will be passed also - without it it'll just quietly drop it.
Something like this ought to work:
RewriteEngine on
RewriteRule ^http://example.com/cms$ http://example.com/cms/index.php?page=filename
...should work.
Have a look at a tutorial with some examples if you're interested in seeing what else you can do.

Resources