mask URL (with parameters) with htaccess - .htaccess

i need to mask a long, stupid php URL with a relatively simple URL with htaccess.
For example, i need to mask -
http://mysite.com/index.php?this=that&this=that&whatevs=this&that=&id=5
with this simple URL -
http://mysite.com/mypage.php
I know php can do the stuff very easily by fetching the content of that page and displaying it. But in my conditions, there are some problems with ajax calls and some other things.
Moreover, i prefer to use htaccess. I did some research but htaccess codes are very confusing to me.

I took the liberty of removing the .php part from mypage.php as it has no real use, and makes the url look less pretty.
RewriteEngine On
RewriteBase /
RewriteRule ^mypage$ /index.php?this=that&this=that&whatevs=this&that=&id=5 [L,QSA]

So you only want redirect http://mysite.com/index.php?this=that&this=that&whatevs=this&that=&id=5 to http://mysite.com/mypage.php?
Then use this RewriteRule:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} this=that&this=that&whatevs=this&that=&id=5
RewriteRule ^index.php$ http://mysite.com/mypage.php? [L,R=301]

Related

htaccess redirect to file except own IP

I'm having a bit of a trouble finding the right way to this:
I'm about to launch a site, www.domain.com, but it's not quite yet ready for the public eyes just yet. So I want to redirect all visitors (except myself) to www.domain.com/teaser.html.
How can I do this, so I keep the site content hidden from visitors, without creating a loop if I redirect base url? How would a htaccess string look like?
If I create something like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^xxx.xxx.xxx.xxx
RewriteRule .* /coming-soon.html [R=302,L]
It just create a loop. How can I do this without creating a loop?
As mentioned in this question.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !=123.45.67.89
RewriteRule index.php$ /teaser.php [R=301,L]

How to prevent duplicate content using htaccess

I'm using a .htaccess to create a clean URL but
when I checked my site
example.com/some-request it has duplicate content with
xyz.com/product_view.php?category_id=some-request
I'm finding hard how to fix this and this is my rewrite rule:
RewriteEngine on
RewriteBase /
RewriteRule ^([0-9a-zA-Z_-]+)$ product_view.php?category_id=$1 [NC,L]
What can be a better solution for this?
You need to externally redirect requests for
example.com/product_view.php?category_id=some-request
to xyz.com/some-request
before your internal rewrite.
Try the following:
RewriteEngine on
RewriteBase /
# Redirect direct requests for the real URL to your desired URL
RewriteCond %{THE_REQUEST} \?category_id=([^\ ]*)
RewriteRule ^product_view\.php /%1? [R=301,L]
# Existing rewrite to real URL
RewriteRule ^([0-9a-zA-Z_-]+)$ product_view.php?category_id=$1 [NC,L]
Bit of an aside... but it's only really duplicate if these URLs get indexed. If you have always linked to your "pretty" URL then the risk is relatively low. This can also be avoided by setting the appropriate rel="canonical" link element on your pages.

.htaccess Rewrite rule doesn't work

I know there are already a ton of these questions but I don't get it...
I'm trying to rewrite my /register.php to /register but I can't.
.htaccess code
RewriteEngine on
RewriteBase /
RewriteRule ^register$ register.php [L]
Does any of you know the answer?
Sorry for the stupid question but I'm kinda new to the whole .htaccess stuff...
Do you have enabled the Apache module mod_rewrite?
I use this pattern to create user-friendly URLs. It's pretty variable. It means, that every address in format letters-numbers with a certain file extension (PHP or HTML) will be translate into letters-numbers.php (for example).
RewriteRule ^([a-z]+)-([0-9]+)\.(php|html)$ /$1-$2.$3 [L]

Using mod_rewrite to mask a directory/file name in a URL

I've taken my site down for some prolonged maintenance and am using mod_rewrite to send all requests to a single page: www.mysite.com/temp/503.php
This is my .htaccess file which works fine.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/temp/503.php [NC]
RewriteRule .* /temp/503.php [R,L]
However, what I'd also like to be able to do is to hide /temp/503.php in the resulting URL from the visitor.
I know this is perhaps trivial and I'm sure fairly simple to achieve, but with my limited mod_rewrite skills I can't seem to get it to work.
Any help would be much appreciated.
Thanks.
Just get rid of the R flag in the rewrite rule, which tells the rule to redirect the request, thus changing the URL in the browser's location bar. So the rule would look like:
RewriteRule .* /temp/503.php [L]
which internally rewrites the requested URI instead of externally telling the browser that it's been moved to a new URL.

How to remove part of a URL using .htaccess

I have a url like this.
/domains/details.php (NOTE: domains is dynamic and can be anything)
How do I remove the domains part from the URL using .htaccess so the actual lookup is:
/details.php
OR it'll be cool if I can get domains into the URL.
/details.php?page=domains
Thanks!
Scott
RewriteEngine on
RewriteBase /
RewriteRule ^([^/]+)/details.php$ /details.php?page=$1 [R=301]
Leave off the [R=301] if you want an internal redirect rather than an actual HTTP redirect.
To preserve existing query parameters you can change the rule to this:
RewriteRule ^([^/]+)/details.php(.*)$ /details.php?page=$1&%{QUERY_STRING} [R=301]
Please try to use the following rules to deal with your last request:
RewriteRule ^(?!domains/.*)([^/]+)/details.php$ domains/details.php?page=$1 [R=301,QSA]
RewriteRule ^domains/details.php$ details.php [NC,L]

Resources