How to Mask URL with Variables (.htaccess) - php

I have viewed similar questions to mine, but it does not pertain to my specific example.
1st URL (what is to be sent to the server and is originally inputted to the URL bar): www.site.com/?variable1=3&variable2=filename.php
2nd URL (what is shown to the user in the URL bar): www.site.com/filename.php
where "filename.php" is the same value as "variable2" in the original URL.
I have seen examples of the opposite, where someone enters the 2nd URL and it replaces it with the 1st URL, but that's not what I want.
Currently what I have:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^/([A-Za-z0-9]+)$ /?CommID=$1&FileName=$2 [NC,L]
which I got the structure from the link provided from this question, but that's not working.
I'm not trying to redirect, just mask the current URL so it is SEO friendly. I need to grab both variables from the URL to have the dynamic content be pulled correctly.
Any ideas?

Try adding the following to your .htaccess file in the root of your domain
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#if the query string has a commid and a fIlename Param
RewriteCond %{QUERY_STRING} ^CommID=([^&]+)&FileName=([^&]+) [NC]
#redirect users to the Pretty URL of www.site.com/variable1/filename.php
RewriteRule .* /%1/%2? [L,R=301]
#for a URL like this in address bar www.site.com/variable1/filename.php
#CommID will contain variable1 and FileName will contain filename.php
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9\.]+)$ /?CommID=$1&FileName=$2 [NC,L]
Edit:
Also redirect users using the query string URL to the pretty URL

Related

How to redirect symbold url generated by search form to original url using .htaccess

When a user types his code in the form , it should open his page.
For eg : when user types CODE: ABCD , then it should open page domainname.com/ABCD, but its giving domainname.com/?code=ABCD.
I used .htaccess
redirect 301 /?code=ABCD /ABCD.
But not working.
Any suggestions to redirect these url with question marks and equals to to plain URL?
You can not access querystrings like that. You need to match against %{QUERY_STRING} using mod-rewrite
RewriteEngine on
RewriteCond %{QUERY_STRING} ^code=([^&]+)
RewriteRule ^$ /%1? [L,R]
This will redirect :
domain.com/?code=helloworld
to
domain.com/helloworld

Automatically redirect to sub-domain with htaccess

I am building a web application in which user is assigned a sub-domain,
Now somehow I am able write an .htaccess code which allow me to get sub-domain and also retrive the content from the database.
.htaccess code :
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^([w]{3,3}[.]{1,1}){0,1}kanai.in$
RewriteCond %{HTTP_HOST} ^([0-9a-zA-Z-]*)[.]kanai.in$
RewriteRule ^$ website.php?url=%1 [NC,L]
orginal link is kanai.in/website.php?url=test
and when I write test.kanai.in it is successfully giving the content which is stored in database,
What I want is that kanai.in/website.php?url=test should automatically redirect to test.kanai.in
You can try this code :
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} url=(.*)
RewriteRule ^website.php$ http://%1.kanai.in? [L,R=301]
We search for url value in query string.
We check that we are on website.php page.
We redirect to the value subdomain and remove query parameters by adding a ?
Those links might help you :
Apache wiki page on how to manipulate the Query String
Test your apache htaccess files online
Guess what, I got the answer,
My htaccess code is correct,
All I have to do is just give it a href link as "http://test3.kanai.in",
So it will redirect to test3.kanai.in and will get the corresponding data from the database.....

Rewrite Mod Still Returns Original URL

Using WamServer , I used Generate It! to over write a URL like
The original URL:
http://localhost/CMS/foo.php?id=2&slug=eyeglasses
The rewritten URL:
http://localhost/2/eyeglasses.php
The rule which site gave me is
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.php$ /CMS/foo.php?id=$1&slug=$2 [L]
but this is still returning original URL
http://localhost/CMS/foo.php?id=2&slug=eyeglasses
Can you please let me know what I am doing wrong?
Update
To be more clarify! I have an index.php which contains a link like
echo ''.$theName.'';
now we user landed in foo.php the URL looks like
http://localhost/CMS/foo.php?id=1&slug=eyeglassess
correct? what I would like to do is displaying the
http://localhost/2/eyeglasses.php
instead of original URL
You need a redirect rule to redirect old URL to pretty URL. This code should be in /CMS/.htaccess
RewriteEngine On
RewriteBase /CMS/
# redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /CMS/+foo\.php\?id=([^\s&]+)&slug=([^\s&]+) [NC]
RewriteRule ^ %1/%2.php? [R=302,L,NE]
RewriteRule ^([^/]+)/([^/]+)\.php$ foo.php?id=$1&slug=$2 [L,NC,QSA]
However it is better to change your link code to new pretty link scheme.

Rewrite URL containing ... in htaccess

Sometimes I find that URLs on my website show up with mywebsite_smf_smf1_session=.
For example:
http://mywebsite.com/index.php​?mywebsite_smf_smf1_session=6goevo28​vf18ubqgb5uh6r08b2&topic=332242​.msg916981
http://mywebsite.com/index.php​?mywebsite_smf_smf1_session=6gqvo88l​u5fnl1qmj7temt0113&topic=316196​.msg931242
The part that contains mywebsite_smf_smf1_session=### shouldn't be in the URL, but the link still redirects to the correct page.
I would like to know how I can use mod_rewrite in .htaccess so that when someone clicks a URL containing mywebsite_smf_smf1_session=###&, it automatically reverts to the version not containing it:
Clicking:
http://
mywebsite.com/index.php​?mywebsite_smf_smf1_session=6gqvo88l​u5fnl1qmj7temt0113&topic=316196​.msg931242
Becomes:
http:// mywebsite.com/index.php​?topic=316196​.msg931242
You can use this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^mywebsite_smf_smf1_session=[^&]*(?:&(.*))?$ [NC]
RewriteRule ^index\.php$ %{REQUEST_URI}?%1 [NE,L,NC,R=302]

Redirect direct image urls requests and pass the original url value in the redirection

Suppose I have someone (or Google Images) trying to access
http://null.com/uploads/someimage.jpg (actually any image in /uploads folder)
I'd want htaccess to redirect the user to
http://null.com/page/
HOWEVER, I need to pass the original url (http://null.com/uploads/someimage.jpg) to the redirection target page as a POST string value which the target php page can use to do stuff with it
Would it be possible to achieve that with htaccess?
UPDATE - and also, it would be interesting to have this working only when the user agent is a human operated browser, not a bot such as google bot, in this way Google and other search engines can crawl images appropriately without the redirection
You may try this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !page\.php [NC]
RewriteCond %{REQUEST_URI} ^/uploads/([^/]+)/? [NC]
RewriteRule . page.php?url=uploads/%1 [L]
Maps silently
http://null.com/uploads/someimage.jpg with or without trailing slash
To:
http://null.com/page.php?url=uploads/someimage.jpg
The string uploads is assumed to be fixed, while someimage.jpg can be any name.
The script name page.php is an example an can be any name also. Replace all instances in the rule-set.
For permanent and visible redirection, replace [L] with [R=301,L]
This answer is according to this description in OP comments:
"...redirect the user to this page and pass the former original url as a post value..."
where page is a script.
NOTE: The parameters passed in the substitution URL can be captured with PHP using $_GET or $_REQUEST, not $_POST.
UPDATE
Here is another version:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !page\.php [NC]
RewriteRule ^(.*)\.jpg page.php?url=$1.jpg [L]
Maps silently
http://null.com/any/number/of/folders/someimage.jpg
To:
http://null.com/page.php?url=any/number/of/folders/someimage.jpg
The rewrite rule is applied only when the incoming URL holds a file with extension jpg at the end of the path.

Categories