htaccess rewrite changing url - .htaccess

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

Related

htaccess disallow redirect specific subdomain

I redirect all virtual subdomains as follows.
RewriteCond %{HTTP_HOST} ^(.+)\.mysite\.com$
RewriteRule ^$ subdomain.php?url=%1 [NC,QSA,L]
This way, I get data from the database. All "virtual subdomains". But there is a real subdomain. Opencart inside (shop.mysite.com)
htaccess directs this as well. How do I prevent it from just redirecting the word "shop".
My Full Code:
Options Indexes SymLinksIfOwnerMatch
RewriteEngine on
directoryindex index.php
Options -indexes
RewriteRule index index.php
RewriteCond %{HTTP_HOST} ^(.+)\.mysite\.com$
RewriteRule ^$ subdomain.php?url=%1 [NC,QSA,L]
# www.site.com >> site.com
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.+)\.mysite\.com$
RewriteRule ^$ subdomain.php?url=%1 [NC,QSA,L]
You can add another condition to exclude the shop subdomain. For example:
RewriteCond %{HTTP_HOST} !^shop\.
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
RewriteRule ^$ subdomain.php?url=%1 [QSA,L]
The ! prefix negates the regex, so the condition is successful when it does not match.
OR, you could use a negative lookahead in your original condition regex. For example:
RewriteCond %{HTTP_HOST} ^((?!shop\.).+)\.example\.com$
RewriteRule ^$ subdomain.php?url=%1 [QSA,L]
However, your directives are also in the wrong order. Your external redirect to remove the www subdomain should appear first, otherwise you need to incorporate another exception into the above rule.
For example:
Options SymLinksIfOwnerMatch
DirectoryIndex index.php
RewriteEngine on
RewriteRule index index.php
# www.site.com >> site.com
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^shop\.
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
RewriteRule ^$ subdomain.php?url=%1 [QSA,L]
You were also adding the Indexes option, only to remove it again with a later directive.

.htaccess redirect addin trailing slash

I have been using the following .htaccess for some time now to redirect non-https to https:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
This week however an SEO expert told me this gives 2 redirects for links like this:
www.example.com/test
The first to http s ://www.example.com/test
The second to http s ://www.example.com/test /
Apparantly this is bad for SEO so I have tried adding a / to the last line, this doesn't work for files, e.g.
www.example.com/test.php => https://www.example.com/test.php/
I have done some searching but I can't seem to find a solution for both issues. Can anyone point me in the right direction?
How about adding a check for the directory in the rule (one for directories and one for files):
<IfModule mod_rewrite.c>
RewriteEngine on
# for directories without trailing slashes
RewriteCond %{HTTPS} !=on [NC]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} [^/]$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]
# for everything else
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
However, there's a good chance this won't work since the redirect from /test to /test/ isn't happening via Rewrite, but via mod_dir's DirectorySlash directive. If you really want to just make this one redirect (I don't think the impact is that serious), then you can turn DirectorySlash off and have that redirect happen via mod_rewrite instead:
DirectorySlash Off
<IfModule mod_rewrite.c>
RewriteEngine on
# for directories without trailing slashes
RewriteCond %{HTTPS} !=on [NC]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} [^/]$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]
# for everything else
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Add trailing slashes for directories that have them missing
RewriteCond %{HTTPS} on [NC]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} [^/]$
RewriteRule ^(.*)$ %{REQUEST_URI}/ [R=301,L]
</IfModule>

Multiple rewrite rules to different pages

I am trying to create multiple rewrite rules, so that a few pages will be redirected to certain pages, and the rest will be redirected to the start page. However, all my pages keep getting redirected to the start page.
This is the code I am using:
RewriteCond %{HTTP_HOST} ^site\.com/category\.php?s=1$ [NC]
RewriteRule (.*) http://site.co.uk/category/? [R=301,L]
RewriteCond %{HTTP_HOST} ^site\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.site\.com$ [NC]
RewriteRule (.*) http://site.co.uk/? [R=301,L]
Edit:
This is the full .htaccess:
Order deny,allow
DirectoryIndex default.php index.php
SetEnv DEFAULT_PHP_VERSION 5
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /flavours\.php?\s=1 [NC]
RewriteRule ^ http://site.co.uk/flavours/? [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?site\.com$ [NC]
RewriteRule ^ http://site.co.uk/? [R=301,L]
This is the link I am trying to access: www.site.com/flavours.php?s=1
HTTP_HOST cannot match REQUES_URI.
You can use:
# specific redirects
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /flavours\.php\?s=1 [NC]
RewriteRule ^ http://site.co.uk/flavours/? [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?flaverco\.com$ [NC]
RewriteRule ^ http://site.co.uk/? [R=301,L]
Make sure to clear your browser cache before testing this.

Domain Management using .htaccess

Suppose all the requests on A.com and B.com end up on the same server, and I want to control the request using htaccess.
The default www content root is /public_html/, but I want A.com requests to be forwarded to /public_html/A/ and B.com requests should be forwarded to /public_html/B/
I came up with this solution:
#/public_html/.htaccess
# A.com
RewriteCond %{HTTP_HOST} ^A.com$
RewriteCond %{REQUEST_URI} !^/A/
RewriteRule (.*) A/$1 [L]
# B.com
RewriteCond %{HTTP_HOST} ^B.com$
RewriteCond %{REQUEST_URI} !^/B/
RewriteRule (.*) B/$1 [L]
and I am having two problems with it:
A.com/A/index.php and A.com/index.php are the same thing, which is not cool! I'd rather the user be 301-ly redirected to the latter whenever he uses the former.
A.com/etc redirects to A.com/A/etc/
Overall, I don't want my visitors to see the /A/ (or /B/) in the URL anyway. Any htaccess solution is welcome.
You can use this code:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
# for external redirection of A.com/A/foo to A.com/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/*(A|B)(?:/(.*)|)\s [NC]
RewriteRule ^ http://%1.com/%2 [R=301,L,NC]
# for internal redirection of A.com/foo to A.com/A/foo
RewriteCond %{HTTP_HOST} ^(A|B)\.com$ [NC]
RewriteRule (?!^(A|B)(?:/(.*)|)$)^.*$ %1%{REQUEST_URI} [L]
Add this to your .htaccess in the same order:
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteRule ^(A|B)(?:/(.*))?$ http://%{HTTP_HOST}/$2 [NC,L,R=301]
#/public_html/.htaccess
# A.com or B.com
RewriteCond %{HTTP_HOST} ^(A|B).com$ [NC]
RewriteRule (.*) %1/$1 [L]
If you do not want A to be redirected to A when accessed like this A.com/B/. or If you do not want B to be redirected to B when accessed like this B.com/A/. You can throw a 404 error.
RewriteCond %{HTTP_HOST} ^A.com$ [NC]
RewriteCond %{REQUEST_URI} ^/B(?:/(.*))? [NC]
RewriteRule ^ - [L,R=404]
RewriteCond %{HTTP_HOST} ^B.com$ [NC]
RewriteCond %{REQUEST_URI} ^/A(?:/(.*))? [NC]
RewriteRule ^ - [L,R=404]
Add the above lines right afterRewriteRule ^ - [L] before RewriteRule ^(A|B)(?:/(.*....

moving to .htaccess

I moved my site to a new php 5.2 server with no access to apache conf files
So I had to put site's rewrite rules into an .htaccess (wildcards is open for the domain)
and I expirience a lot of issues with it now
ORIGINAL WAS
Options +FollowSymLinks
rewriteEngine on
rewriteCond $1 !^pages/
rewriteCond %{HTTP_HOST} !^www\.mydomain\.com
rewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com
rewriteRule (.*) /pages/%1/$1
rewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /pages/
rewriteRule ^pages/([^/]+)/(.*)$ http://$1.mydomain.com/$1 [R=301,L]
What changes Do I need to make ?
the whole point is that I need to redirect:
www.mydomain.com/pages/XXXXX.php ==>
XXXXX.mydomain.com/...whatever....
but also
www.mydomain.com/pages/XXXXX/...whatever.... ==> XXXXX.mydomain.com/...whatever....
The rules below should do what you requested.
However, in the first example, where does the whatever appear in the original request to www.mydomain.com/pages/XXXXX.php.
If the whatever refers to the query string, then it should work.
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$ [NC]
#redirect www.mydomain.com/pages/XXXXX.php to XXXXX.mydomain.com/
RewriteRule ^pages/(.+)\.php http://$1.mydomain.com/ [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$ [NC]
#redirect www.mydomain.com/pages/XXXXX/.whatever.. to XXXXX.mydomain.com/...whatever
RewriteRule ^pages/([^/]+)/(.*) http://$1.mydomain.com/$2 [R=301,L]
You could try the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^pages/
RewriteRule ^pages/([^/]+)(?:\.php|/(.*))$ http://$1.mydomain.com/$2 [R=301,L]
RewriteCond %{REQUEST_URI} !^pages/
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com
RewriteRule (.*) /pages/%1/$1 [L]
</IfModule>
And one more: please make sure whether you REALLY NEED the Options +FollowSymLinks directive as this might cause an internal error if not allowed or already set in httpd.conf

Resources