How to use mod_rewrite and still show requested URL? - .htaccess

I wish to have request for http://user1.domain.com :
return the output from http://www.domain.com/site-client/site.php?site=user1
AND user still see the URL 'http://user1.domain.com' in the browser
Right now I have the following .htaccess:
RewriteEngine On
RewriteBase /
# Protect the htaccess file
<Files .htaccess>
Order Allow,Deny
Deny from all
</Files>
# Disable directory browsing
Options All -Indexes
RewriteCond %{HTTP_HOST} !^www.domain.com
RewriteCond %{HTTP_HOST} !^admin.domain.com
RewriteCond %{HTTP_HOST} ([^.]+).domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/site-client/site.php?site=%1 [L]
The successfully calls the http://www.domain.com/site-client/site.php?site=user1 URL, but it shows it to the user.
How can I avoid this?

You need to get rid of the http://www.domain.com part of yor RewriteRule's target. It tells mod_rewrite that you want a 302 redirect. So your rule should look like this:
RewriteCond %{HTTP_HOST} !^www.domain.com
RewriteCond %{HTTP_HOST} !^admin.domain.com
RewriteCond %{HTTP_HOST} ([^.]+).domain.com [NC]
RewriteCond %{REQUEST_URI} !^/site-client/site.php
RewriteRule ^(.*)$ /site-client/site.php?site=%1 [L]
EDIT: You'll also need a check to keep the rule from looping

Related

How to allow access to files for all sites while maintaining htaccess redirection

I wanted to keep the redirect rule. but I don't want to have to add site by site so that they can display the image. how can i change this rule so that it can do this trick? thanks in advance. Any help is welcome :)
actual htaccess
<Files ~ "\.(jpg|jpeg|png|gif)$">
Order allow,deny
allow from all
</Files>
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)fbclid=
RewriteRule ^ / [L,R=permanent]
RewriteCond %{HTTP_REFERER} !^https://mysite,com [NC]
RewriteCond %{HTTP_REFERER} !^https://www.facebook,com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^https://www.facebook,com/ [NC]
RewriteCond %{HTTP_REFERER} !^https://externalsite,com/ [NC]
RewriteCond %{HTTP_REFERER} !^http://externalsite2,com/ [NC]
RewriteCond %{HTTP_REFERER} !^https://mysite,com.*$ [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ https://mysite,com [NC,R,L]
<IfModule mod_php4.c>
php_value engine off
</IfModule>
<IfModule mod_php5.c>
php_value engine off
</IfModule>
<Files ~ "\.((php[0-9]?)|p?html?|pl|sh|java|cpp|c|h|js|rc)$">
Order allow,deny
Deny from all
</Files>
RewriteCond %{HTTP_REFERER} !^https://mysite,com [NC]
RewriteCond %{HTTP_REFERER} !^https://www.facebook,com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^https://www.facebook,com/ [NC]
RewriteCond %{HTTP_REFERER} !^https://externalsite,com/ [NC]
RewriteCond %{HTTP_REFERER} !^http://externalsite2,com/ [NC]
RewriteCond %{HTTP_REFERER} !^https://mysite,com.*$ [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ https://mysite,com [NC,R,L]
I wanted to give full access to other sites to incorporate my content ... redirecting if you access the file directly at example.com/uploads/test.gif
If you simply want to block direct access (in which case the Referer header is empty) and allow all other sites to link to your images (basically the opposite of "hotlink protection") then you can replace the above rule (that redirects such requests to the root) with the following:
RewriteCond %{HTTP_REFERER} ^$
RewriteRule \.(jpg|jpeg|png|gif)$ https://example.com/ [NC,R,L]
This redirects all direct requests to the root / home page.
(You had erroneous commas , in your original directives that would have prevented this from working?!)
HOWEVER, basing this redirect on the HTTP Referer is unreliable - you will get false positives. The Referer is sent by the browser - so the user can control (and suppress) what is sent. The website that is linking to you can also set a referrer-policy that suppresses the HTTP Referer being sent - so all requests from some sites might look like direct requests anyway and end up being blocked. There is no way around this.

Moving an entire directory using .htaccess fails for individual files

I'm trying to 301 .htaccess an entire directory INCLUDING all the files within that directory.....if I use this string:
RewriteRule ^old/(.*)$ /new/$1 [L,R=301]
Running the below is successful....
website.com/old/ redirects to website.com/new/
Running the below is NOT successful....(error 404)
website.com/old/page-123.php does NOT redirect to website.com/new/
So, individual files within that directory throw a 404 error....any ideas why?
Worth mentioning as well that I have the following rules above:
<files .htaccess>
Order allow,deny
Deny from all
</files>
Options +FollowSymLinks
RewriteEngine On
# All pages www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# below to force https
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.my-site.com%{REQUEST_URI} [NE,R=301,L]
This worked
RewriteRule ^old/* https://www.my-site.com/new/ [R=301,L]
Hope that helps someone :)

How to fix a mod_rewrite RewriteRule in HTACCESS

I'm writing a few RewriteRules, and my most basic rewrite returns a 404 error. Here's my code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule s/(.*)/(.*)/$ /page.php?s=$2 [NC,L]
RewriteRule ^submit/$ submit.php [R,L]
# Options All -Indexes
<files .htaccess>
order allow,deny
deny from all
</files>
RewriteRule ^submit/$ submit.php [R,L] is where I'm having trouble.
When I visit domain.com/submit/, my server returns a 404 error saying, "The requested URL /submit/ was not found on this server." It's like the server did not even look at my HTACCESS file. The other RewriteRules work perfectly.
Am I missing something?
You may try this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !page\.php [NC]
RewriteRule ^s/(.*)/(.*)/$ /page.php?s=$2 [NC,L]
RewriteCond %{REQUEST_URI} !submit\.php [NC]
RewriteRule ^submit submit.php [R,L,NC]
That's it according to the information in the question.
Can't guess what's the first rule for as there are no URL examples to determine the pattern to be matched by the regex or the location of the script.
Something similar happens with the second rule because there is no way to confirm where the script (submit.php) is located as, again, there are no URL examples in the question.

htaccess rewrite changing url

I'm having trouble connecting the dots here. Is there an in between step for changing the url within the .htaccess. This is what I have so far.
moniquetrinidadjewelry.com/necklace/product.php?id=17&product_name=enchanting%2520pearl
and the rewrite
RewriteEngine On
RewriteRule ^$necklace/([a-zA-Z]+)/([0-9]+)/$ product.php?id=$1&product_name=$2
My goal and what I believed to be set as above is a url of moniquetrinidadjewelry.com/necklace/id/product_name
I'm not entirely sure where the issue is coming from. There is no change within the address bar at refresh, reload or a start over of browse.(ie. home necklace> ect.)
Am I missing something important or have I skipped a step not within the htaccess itself?
Edit:
# Use PHP5.3 Single php.ini as default
AddHandler application/x-httpd-php53s .php
# Always use www in the domain
# Replace 'moniquetrinidadjewelry' with your domain name
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z.]+)?moniuetrinidadjewelry.com$ [NC]
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteRule .? http://www.%1moniquetrinidadjewelry.com%{REQUEST_URI} [R=301,L]
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=([0-9]+)&product_name=([a-zA-Z]+)$
RewriteRule product.php necklace/%1/%2/
# For security reasons, Option followsymlinks cannot be overridden.
#Options -MultiViews +FollowSymlinks
Options -MultiViews +SymLinksIfOwnerMatch
RewriteEngine on
# Always use www in the domain
# Replace 'moniquetrinidadjewelry' with your domain name
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z.]+)?moniuetrinidadjewelry.com$ [NC]
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteRule .? http://www.%1moniquetrinidadjewelry.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteRule ^/?(.*/?)index\.(htm|html|php) /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
ErrorDocument 404 /
<Files error_log>
order allow,deny
deny from all
</Files>
# Ultimate htaccess Blacklist 2 from Perishable Press
# Deny domain access to spammers and other scumbags
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} ADSARobot|ah- ha|almaden|aktuelles|Anarchie|amzn_assoc|ASPSeek|ASSORT|ATHENS|Atomz|attach|attache|autoema ilspider|BackWeb|Bandit|BatchFTP|bdfetch|big.brother|BlackWidow|bmclient|Boston\ Project|BravoBrian\ SpiderEngine\ MarcoPolo|Bot\ mailto:craftbot#yahoo.com|Buddy|Bullseye|bumblebee|capture|CherryPicker|ChinaClaw|CICC|clip ping|Collector|Copier|Crescent|Crescent\ Internet\ ToolPak|Custo|cyberalert|DA$|Deweb|diagem|Digger|Digimarc|DIIbot|DISCo|DISCo\ Pump|DISCoFinder|Download\ Demon|Download\ Wonder|Downloader|Drip|DSurf15a|DTS.Agent|EasyDL|eCatch|ecollector|efp#gmx\.net|Email\ Extractor|EirGrabber|email|EmailCollector|EmailSiphon|EmailWolf|Express\ WebPictures|ExtractorPro|EyeNetIE|FavOrg|fastlwspider|Favorites\ Sweeper|Fetch|FEZhead|FileHound|FlashGet\ WebWasher|FlickBot|fluffy|FrontPage|GalaxyBot|Generic|Getleft|GetRight|GetSmart|GetWeb!|Get WebPage|gigabaz|Girafabot|Go\!Zilla|Go!Zilla|Go-Ahead- Got-It|GornKer|gotit|Grabber|GrabNet|Grafula|Green\ Research|grub- client|Harvest|hhjhj#yahoo|hloader|HMView|HomePageSearch|http\ generic|HTTrack|httpdown|httrack|ia_archiver|IBM_Planetwide|Image\ Stripper|Image\ Sucker|imagefetch|IncyWincy|Indy*Library|Indy\ Library|informant|Ingelin|InterGET|Internet\ Ninja|InternetLinkagent|Internet\ Ninja|InternetSeer\.com|Iria|Irvine|JBH*agent|JetCar|JOC|JOC\ Web\ Spider|JustView|KWebGet|Lachesis|larbin|LeechFTP|LexiBot|lftp|libwww|likse|Link|Link*Sleuth |LINKS\ ARoMATIZED|LinkWalker|LWP|lwp-trivial|Mag-Net|Magnet|Mac\ Finder|Mag-Net|Mass\ Downloader|MCspider|Memo|Microsoft.URL|MIDown\ tool|Mirror|Missigua\ Locator|Mister\ PiX|MMMtoCrawl\/UrlDispatcherLLL|^Mozilla$|Mozilla.*Indy|Mozilla.*NEWT|Mozilla*MSIECrawler| MS\ FrontPage*|MSFrontPage|MSIECrawler|MSProxy|multithreaddb|nationaldirectory|Navroad|NearSite |NetAnts|NetCarta|NetMechanic|netprospector|NetResearchServer|NetSpider|Net\ Vampire|NetZIP|NetZip\ Downloader|NetZippy|NEWT|NICErsPRO|Ninja|NPBot|Octopus|Offline\ Explorer|Offline\ Navigator|OpaL|Openfind|OpenTextSiteCrawler|OrangeBot|PageGrabber|Papa\ Foto|PackRat|pavuk|pcBrowser|PersonaPilot|Ping|PingALink|Pockey|Proxy|psbot|PSurf|puf|Pump| PushSite|QRVA|RealDownload|Reaper|Recorder|ReGet|replacer|RepoMonkey|Robozilla|Rover|RPT- HTTPClient|Rsync|Scooter|SearchExpress|searchhippo|searchterms\.it|Second\ Street\ Research|Seeker|Shai|Siphon|sitecheck|sitecheck.internetseer.com|SiteSnagger|SlySearch|Smar tDownload|snagger|Snake|SpaceBison|Spegla|SpiderBot|sproose|SqWorm|Stripper|Sucker|SuperBot |SuperHTTP|Surfbot|SurfWalker|Szukacz|tAkeOut|tarspider|Teleport\ Pro|Templeton|TrueRobot|TV33_Mercator|UIowaCrawler|UtilMind|URLSpiderPro|URL_Spider_Pro|Vac uum|vagabondo|vayala|visibilitygap|VoidEYE|vspider|Web\ Downloader|w3mir|Web\ Data\ Extractor|Web\ Image\ Collector|Web\ Sucker|Wweb|WebAuto|WebBandit|web\.by \.mail|Webclipping|webcollage|webcollector|WebCopier|webcraft#bea|webdevil|webdownloader|We bdup|WebEMailExtrac|WebFetch|WebGo\ IS|WebHook|Webinator|WebLeacher|WEBMASTERS|WebMiner|WebMirror|webmole|WebReaper|WebSauger|W ebsite|Website\ eXtractor|Website\ Quester|WebSnake|Webster|WebStripper|websucker|webvac|webwalk|webweasel|WebWhacker|WebZIP|W get|Whacker|whizbang|WhosTalking|Widow|WISEbot|WWWOFFLE|x-Tractor|^Xaldon\ WebSpider|WUMPUS|Xenu|XGET|Zeus.*Webster|Zeus [NC]
RewriteRule ^.* - [F,L]
If I understand correctly, you must first capture the query string parts in a RewriteCond and then use that in a RewriteRule
RewriteCond %{QUERY_STRING} ^id=([0-9]+)&product_name=([a-zA-Z]+)$
RewriteRule product.php necklace/%1/%2/ [R,L]
This rule will fire, when there's a URL product.php?id=17&product_name=enchanting%2520pearl and rewrite it to necklace/17/enchanting%2520pearl. The client is redirected and the browser bar should show the new URL.
If you want it the other way round, this one should work
RewriteRule necklace/([0-9]+)/([a-zA-Z]+) product.php?id=$1&product_name=$2

htaccess mod_rewrite no duplicate content

I have written a rewrite rule to get all pages looking like
dealer_listings.php?territory=arizona
to be accessible like this
wholesale-parts/arizona/
I would like the ugly url to redirect to the seo friendly one as well.
Options -MultiViews
RewriteEngine on
<FILES .htaccess>
order allow,deny
deny from all
</FILES>
RewriteRule ^([^/\.]+)/+([^/\.]+)/([^/\.]+)/?$ bussiness_profile.php? what=$1&type=$2&dealer=$3 [L]
rewriterule ^wholesale-parts/([^/\.]+)/?$ dealer_listings.php?territory=$1 [R=301,L]
rewritecond %{http_host} ^www.elite-dealers.com [nc]
rewriterule ^(.*)$ http://elite-dealers.com/$1 [r=301,nc]
RewriteRule ^(([^/]+/)*)index\.php http://elite-dealers.com/$1 [R=301,L]
Add the following code to your .htaccess file to redirect the ugly URL to the SEO friendly one.
#if the request is for dealer_listings
RewriteCond %{REQUEST_URI} ^/dealer_listings\.php$ [NC]
#and it has a territory parameter
RewriteCond %{QUERY_STRING} ^territory=([^&]+) [NC]
#then 301 redirect to the SEO friendly version
RewriteRule . wholesale-parts/%1/? [L,R=301]

Resources