.htaccess redirect/rewrite help needed - .htaccess

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.

Related

redirect old wordpress ?page_id= to non-wordpress site

I used to have a WP site that I converted to a standard html site. Problem is I found doing a google search that instead of http://www.genealogyinc.com it was returning http://www.genealogyinc.com/?page_id=21, I dont know how many pages are like this but am trying to find a htaccess workaround, all the ones I found online give me 500 server errors.
Need a rewrite for any ?page_id= cause I dont know how many other numbers are out there.
Thanks
Off the top of my head, without testing, it would be something like this.
The first line looks for the page_id querystring parameter, and if it meets it, it should pass on to the second line. The rewrite rule I have below may need some tweaking, but I hope this helps you.
RewriteCond %{QUERY_STRING} page_id=(.*)$
RewriteRule $ /? [R=301,L]

Rewrite rule in htaccess to keep parameters

I've written the following rewrite rule which works fine when no parameters (no page numbers, no products per page, no sort order etc)
RewriteRule ^(?!bench/).*cat_2.html(\.[a-z]{3,4})?(.*) "http\:\/\/www\.mysite\.co\.uk\/bench\/cat_2\.html\?mode\=allBrands" [R=301,L]
This makes sure the URL is optimized on Googles results. So
http://www.mysite.co.uk/bench-clothing/cat_2.html?mode=allBrands#
gets changed to
http://www.mysite.co.uk/bench/cat_2.html?mode=allBrands
mode=allBrands will always be set.
So if i click on a link to go to
http://www.mysite.co.uk/bench-clothing/cat_2.html?mode=allBrands&ppp=64&sort=desc&page=2
it gets redirected to
http://www.mysite.co.uk/bench/cat_2.html?mode=allBrands
which is the first page.
Any help would be great.
change [R=301,L] to [R=301,L,QSA]
More info : https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_qsa
You need to add a QSA flag to your rewrite rule, so that the brackets look like this:
[R=301,L,QSA]
This tells apache to append any existing query string to the new query string in the target (mode=allBrands).

Problems with a simple URL rewrite (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).

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.

URL Rewriting based on form input

I'm creating a frontpage for my website with a single form and input text, Google-style. It's working fine, however, I want to generate a pretty URL based on the input. Let's say, my input is called "id", and using the GET method of form, and the action defined to "/go/", on submission, the URL will be:
site.com/go/?id=whateverIType
and I want to change it to
site.com/go/whateverIType
I was thinking on Mod Rewrite, but if the user put something in the URL, like:
site.com/go/?dontwant=this&id=whateverIType&somemore=trash
I want to ignore the other variables but "id", and rewrite the rule.
What's the better way of get this done? Thanks in advance!
PS: I'm using CodeIgniter, maybe there's something I can use for it as well. I already have a controller for "go".
I'm not familiar with CodeIgniter, but you can try the following RewriteRule
RewriteEngine on
RewriteCond %{REQUEST_URI} ^\/go\/
RewriteCond %{QUERY_STRING} id=([^&]*)
RewriteRule (.*) /go/%1? [L,R]
The %1 references the regex group from the previous RewriteCond, and the trailing ? will strip the querystring from the redirected URL.
Hope this helps.
Mod_rewrite supports conditions and rules with RegEx, so you could have a rule that matched the ?id=XXXX, that would extract it from the URL (keeping the other parameters), and rewrote the URL accordingly.
However... I don't think you want to do this, because if you rewrite the URL to be /go/Some+Search+Query, you won't be able to pick it up with say, PHP, without parsing the URL out manually.
It's really tough to have custom, SEO-friendly URLs with user input, but it is technically possible. You're better off leaving in the ?id=XXX part, and instead, using mod_rewrite in the opposite approach... take all URLs that match the pattern /go/My+Search+Terms and translate that back into something like ?id=My+Search+Terms, that way you'll be able to easily parse out the value using the URL's GET parameters. This isn't an uncommon practice - Google actually still uses URL parameters for user input (example URL: http://www.google.com/search?q=test).
Just keep in mind that mod_rewrite rewrites the URL before anything else (even PHP), so anything you do to the URL you need to handle. Think of mod_rewrite as a regular expression-based, global "Find and Replace" for URLs, every time a page is called on the server. For example, if you remove the query string, you need to make sure your website/application/whatever accounts for that.
In application/config/routes.php
$route['go/(:any)'] = "go/index/$1";
Where go is your controller and index is the index action.
http://codeigniter.com/user_guide/general/routing.html
You can use something like this in your .htaccess if you aren't already:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Resources