.htaccess - How do you do this? http://www.example.com/domain.com - .htaccess

i want to use somthing like http://www.example.com/domain.com instead of http://www.example.com/index.php?url=domain.com.
how can I do this using .htaccess?
update: i finally figured it out. :)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1
http://www.pagerankcheckertool.com/facebook.com

RewriteEngine on
RewriteRule ^(.+)$ index.php?url=$1

Depending on your needs, it may not be such a good idea to have everything rewritten as per your example, e.g. even a www.example.com/index.html would be rewritten to www.example.com/index.php?url=index.html so i'd recommend you use an initial sub folder or something in the url to seperate your rewritten urls from anything else.. i.e. www.example.com/urls/domain.com
To accomplish that you could setup a rewrite rule.. (assuming you have mod_rewrite active)
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^urls/(.+) /index.php?url=$1 [NC]
that basically means any url that begins with urls/ and has one or more characters following.. the brackets around the .+ will 'group' that element and allow you use it again with the $1
Hope that solves what you want to accomplish!

Related

Straight Rewrite Rule for htaccess

I was looking for a simple .htaccess configuration that just convert /some_uri to /some_uri.php. Most examples in community are more complicated than I require. I was trying the following, but it didn't work:
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteRule ^(.*)$ ./$1.php
Please help. Thank you.
You need to make sure that the rule does not match itself. In other words, you need to make sure the rule does not match if the url already ends on php. Besides that you probably want to prevent that the rule matches if it already points to a file that exists, so that you can normally serve css/js/images without it trying to append php to it.
RewriteCond %{REQUEST_URI} !\.php$ #Does not end with .php
RewriteCond %{REQUEST_FILENAME} !-f #Requested url is not an existing file
RewriteRule ^ %{REQUEST_URI}.php [L]

domain regex for .htaccess

Firstly, sorry for my bad English.
I want config my .htaccess to rewrite URL.
example.com/company1.com
instead example.com/sub=company1.com
My .htaccess now:
RewriteEngine On
RewriteRule ^([a-z_]+)/?$ index.php?sub=$1
I was search in stackoverflow.
If i using (.*) regex for all charaters or ([a-z\.]+) for include "dot" character in domain string ( company1.con), my skin was broken.
My temporary solution is use ([a-z_]+) with http://example.com/company1_com instead
http://example.com/company1.com
It's bad solution :(
So, please give me regex for this problem.
Thanks.
Rewriting for Apache is described in mod_rewrite.
For you, as long as you ignore possible GET-parameters or paths, it should be
RewriteEngine On
RewriteRule ^/([^?/]+) /index.php?sub=$1 [L]
I guess it was broken because either you were missing the "/" before index.php, there is a longer path in GET ( example.com/company1.com/css/style.css ) or you submit a form ( example.com/company1.com?a=foo&b=bar ).
You need to prevent the index.php from looping:
RewriteEngine On
# let index.php pass through, thus stopping the rewrite loop
RewriteRule index.php - [L]
# route everything to index.php
RewriteRule ^(.*)$ /index.php?sub=$1 [L]
You could also do a check for existing resources first. Since index.php exists, that would also break the loop. This would make it so if you're requesting static content like javascript or css, it won't get routed through index.php:
RewriteEngine On
# request isn't for an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# route everything to index.php
RewriteRule ^(.*)$ /index.php?sub=$1 [L]
Try this one
RewriteEngine On
RewriteRule ^(.*)$ index.php?sub=$1

Mod_rewrite /category.htm into /category

After all those mod rewrite topics here at stackoverflow, I still haven't find an answer to my question. I have a topsite, and basically all I want to do is to change /index.php?method=in&cat=Half+Life (the "+" is a space) into /Half-Life .
Until now, I've succeeded changing /index.php?method=in&cat=Half+Life into /Half+Life.htm .
What I want is to make the .htm disappear and to change the "+" into "-".
Here's my code with what I'm working on in my .htaccess file :
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)\.htm$ /index.php?cat=$1 [L]
One more question: Is it more, how should I call it, "SEO-friendly", if I do it this way?
Thanks!
The problem with removing the .htm from it is that it then rewrites every single url. That would include urls for files that actually exist (like index.php). There are a couple approaches that you could take.
Assume that all your actual files have a dot (.) in the file name, rewrite everything else. If you do actually have files or directories that are named without an extension, this won't work. Or if you want some of your categories to have dots.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ([^\.]+)$ /index.php?cat=$1 [L]
You can have your rewrite rule ignore files that actually exist
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php?cat=$1 [L]
I think it would be easier to change the php to replace the dashes with spaces, but if you really want to use htaccess:
RewriteRule (.*)-(.*)\.(htm)$ "$1 $2.$3"
RewriteRule (.*)\.htm$ /index.php?cat=$1 [L]
What you seem to be asking is to translate SEO-style URIs to internal parameter based ones. Unfortunately, the rewrite engine doesn't support global substitution so you need to cludge this, for example:
RewriteBase /
RewriteRule (\w+)$ index.php?cat=$1 [L]
RewriteRule (\w+)+(\w+)$ index.php?cat=$1-$2 [L]
RewriteRule (\w+)+(\w+)+(\w+)$ index.php?cat=$1-$2-$3 [L]
# and so on ..
But the easiest way to do this is to use a general catch all:
RewriteRule ([-+\w]+)$ index.php?cat=$1 [L]
And use a str_replace or preg_replace on your $_GET['cat'] inside your inddex.php.

301 Redirect with .htaccess

Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^category?$
RewriteRule ([^/]+)/([^/]+)/([^/]+)/$ http://domain.com/$3/ [L,R=permanent]
Currently I have the following redirection and it is working like a charm. Now I want to make sure that the link does not begins with /category/ therefore I have inserted the condition. Unfortunately it does not seems to work. Please help. Thanks.
Another question is, how to make that the end permalink that is between the slash is selected to be redirected only. For example, I may have links like http://domain.com/downloads/26-fine-wallpapers/ and http://domain.com/downloads/icons/35-nice-icons/ and I want links like these to be redirected to http://newdomain.com/35-nice-icons/ and http://newdomain.com/26-fine-wallpapers/
I am using wordpress actually.
According to your description you only have two path segments. So your pattern should be:
RewriteRule ^([^/]+)/([^/]+)/$ http://example.com/$3/ [L,R=permanent]
And to exclude /category/…, you can either check the request URI path in REQUEST_URI:
RewriteCond %{REQUEST_URI} !^/category/
RewriteRule ^([^/]+)/([^/]+)/$ http://example.com/$3/ [L,R=permanent]
Or you check the matched value of the first group:
RewriteCond $1 !=category
RewriteRule ^([^/]+)/([^/]+)/$ http://example.com/$3/ [L,R=permanent]
I think you just need a prefixing /:
RewriteCond %{REQUEST_URI} !^/category?$

htaccess rewrite for query string

Ok, im pretty new at this and I would really appreciate some help, thanks!
How can i rewrite this in .htaccess correctly?
So I have a query string in my url:
/?url=contact
All i want to do is remove the query string
/contact
Help? I scoured google and I'm learning the syntax right now, but the fact remains..I dont know how to do it just yet. Thanks to all
This was my solution:
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
Try this:
RewriteEngine On
RewriteRule ^(.*)$ /index.php?url=$1 [L]
To a user on your site, they will see and navigate to this:
http://example.com/contact
But the real page would be something like this:
http://example.com/index.php?url=contact
This bit, [L], tells the server that this is the last line of the rewrite rule and to stop.
RewriteCond %{QUERY_STRING} url=(.*)
RewriteRule index.html %1
(or whatever if it's not index.html, index.php, whatever)
You need to capture the query string, which is not looked at by RewriteRule normally, and use the %1 back reference, not $1 as you would in a capture in a RewriteRule
Before: https://example.com/index.php?user=robert
RewriteEngine On
RewriteRule ^user/([^/]+)?$ index.php?user=$1 [L,QSA]
After: https://example.com/user/robert

Resources