/    Sign up×
Community /Pin to ProfileBookmark

How to set url rewriting in .htaccess file?

I have a redirect script that redirects with this url

[B][url]http://my-domain.com/URL.php?url=http://www.google.com[/url][/B]

I want to rewrite the url as

[B][url]http://my-domain.com/?url=http://www.google.com[/url][/B]

or

[B][url]http://my-domain.com/?http://www.google.com[/url][/B]
I know this can be done with .htaccess so i tried some codes myself. I created a .htaccess file in my web directory and i put some code eg:

# Prevent Apache from serving .ht* files:

<FilesMatch “^.ht”>
Order allow,deny
Deny from all
</FilesMatch>
RewriteEngine on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^url/(.*)$ /URL.php?url=$1 [L]

But still it’s not working, I mean it works when i use yoursite.co/url/?url=http://www.google.com but script is not working i mean it’s not redirecting, when i remove the htaccess code it’s works fine tell me what’s i’m doing wrong ?

to post a comment
SEO

3 Comments(s)

Copy linkTweet thisAlerts:
@rootOct 18.2017 — I am sure that you have to have a reference to something like [COLOR="#000080"][B]RewriteCond %{REQUEST_URI}[/B] <regexp> [NC][/COLOR] or similar in there.

I notice that you have Two lines with the same command, RewriteEngine on
RewriteEngine On
when only the one is needed...

I don't understand why you want to add this step, its adding processing time and that is reflected in response time and workload of the server. I know .htaccess has a purpose and is very useful tool and can be quite cryptic at times, but use it when needed.

If you are using a rewrite tool then you can do this without having a php script by using rewrite conditions to take the domain name in the request string, you have some sort of instruction as a rewrite rule [B]RewriteRule ^url/(.*)$ /URL.php?url=$1 [L][/B] and I am sure something is missing from here. think it might be the %{THE_REQUEST} that should be before the rewrite rule itself.

I am not so hot on .htaccess as I have little use for it, so someone who is a more frequent flyer can assist with this unless what I have prodded at the code has dropped a few pennies for you.
Copy linkTweet thisAlerts:
@MoulanaRafiOct 23.2017 — any other method?
Copy linkTweet thisAlerts:
@vicky_shahOct 24.2017 — Examples of redirects :

Redirect /index.html /new/

Redirect /index.html /default.html

Redirect /private/ http://www.example.com/private/

Redirect /img/logo.gif http://www.example.com/images/logo.gif

Another form of redirection uses the RedirectMatch command:

RedirectMatch "^/oldfile.html/?$" "http://example.com/newfile.php"

You can also redirect 404 errors. Instead of throwing a 404 page, this redirects to the homepage of the website.

ErrorDocument 404 http://example.com/

Redirect non-existing pages to index.php

If a visitor attempts to access a page that doesn't exist, they are presented with a 404 error. You can instead redirect any request to a non-existing page to your index.php file (or any index file) by adding the following code in your .htaccess:

Options +SymLinksIfOwnerMatch

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

If your index page isn't index.php, just change the last line to your actual index file. Then the visitor is redirected back to your home page.

Automatically loading a subdirectory :

This example redirects the ROOT domain's URL to any subdirectory. In this example, it automatically loads example.com/subdir1:

RewriteEngine on

RewriteRule ^$ /subdir1/ [L]

Forcing www in the URL :

RewriteEngine On

RewriteCond %{HTTP_HOST} ^example.com

RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Removing www in the URL :

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www.example.com

RewriteRule (.*) http://example.com/$1 [R=301,L]
×

Success!

Help @V_MPIRE spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...